Solved

Need help with tricky query

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

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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

728 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