Solved

Need help with tricky query

Posted on 2014-12-10
3
111 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
3 Comments
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 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 12

Accepted Solution

by:
Phil Phillips earned 250 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now