?
Solved

Need help with tricky query

Posted on 2014-12-10
3
Medium Priority
?
122 Views
Last Modified: 2014-12-11
I have a query that when run will replace all except one duplicate in my table column with pound signs "####".
That is exactly what I want. (My query is displayed at the very bottom of this post)

So for example here is my table data :

888659000  IAAE  2014-12-07 08:20:57.407
666659700  IAAE  2014-12-07 08:20:58.408
666659700  IAAE  2014-12-07 08:20:59.409

After I run my query I am left with this table data, which is correct.

888659000  IAAE  2014-12-07 08:20:57.407 -- Just one IAAE record is left, which is what I want
666659700  ####  2014-12-07 08:20:58.408
666659700  ####  2014-12-07 08:20:59.409

Now the query will no longer work if the records have duplicate dates. For example look at the data below. The first two records has the same date, so the query no longer works.
I need help fixing the query.

888659000  IAAE  2014-12-07 08:20:57.407
666659700  IAAE  2014-12-07 08:20:57.407
666659700  IAAE  2014-12-07 08:20:59.409

Here is the sql query :

update t
 set code = 'I###'
 from Goop t
 where t.DateEntered > ( select min(x.DateEntered) from Goop x  WHERE Code = 'IAAE') AND Code = 'IAAE'

Can someone help fix the query, so that only one IAAE records remains? I have attached the database table scripted. Please see attachement.
ThisDatabaseTable.txt
0
Comment
Question by:brgdotnet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 1000 total points
ID: 40493237
this should do:
with data as (
  select snn ,code , DateEntered 
 ,  row_number() over ( partition by code order by dateentered ) rn
  from yourtable
  where code = 'IAAE'
)
update data 
   set code = '####'
where rn > 1

Open in new window

0
 
LVL 14

Accepted Solution

by:
Phil Phillips earned 1000 total points
ID: 40493251
Maybe something like:

UPDATE Goop
 SET Code = '####'
WHERE
 Code = 'IAAE' AND
 SSN <> (SELECT TOP 1 SSN FROM Goop WHERE Code = 'IAAE' ORDER BY DateEntered);

Open in new window

0
 
LVL 2

Author Closing Comment

by:brgdotnet
ID: 40493702
Thank you guys, and God Bless you!!!
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question