Solved

Need help with tricky query

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how the fundamental information of how to create a table.

696 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