Solved

Need help with tricky query

Posted on 2014-12-10
3
118 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 13

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

821 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