Solved

I need help with my update statement

Posted on 2014-12-08
2
151 Views
Last Modified: 2014-12-08
I have a table named Woop with some sample data as shown below.
The table has duplicate Code values. In this case it has two
records with a code value of IAEE.

I want to keep only one record which contains a code of IAEE.
I need to keep the IAEE record which has the earliest DateEntered date.
For the other records which contain a code of IAEE I want to change their codes to
pound signs ####

So here is the table data before


Code      DateEntered
IXXX      2014-12-07 08:20:58.407
IAEE      2014-12-07 08:35:18.853
IAEE      2014-12-07 08:36:28.763


Here is how the table should look after I run the update statement below

Code      DateEntered
IXXX      2014-12-07 08:20:58.407
IAEE      2014-12-07 08:35:18.853
####      2014-12-07 08:36:28.763


update t
    set Code = '####'  
    from Woop t
    where t.DateEntered > ( select min(x.DateEntered) from Woop x )  AND Code = 'IAAE' -- AND SUBSTRING(t.Code,1,1) = 'A'
END


My update statement is broken, because it changes both records to have a code value of ####. What is wrong with my update statement?

Here is what is ultimately produced, and which is wrong.

Code      DateEntered
IXXX      2014-12-07 08:20:58.407
####      2014-12-07 08:35:18.853
####      2014-12-07 08:36:28.763
0
Comment
Question by:brgdotnet
2 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 40488183
If you want to update just for IAAE then you need to add it to the subquery:
update t
    set Code = '####'  
    from Woop t
    where t.DateEntered > ( select min(x.DateEntered) from Woop x where x.Code=t.Code)  AND Code = 'IAAE' -- AND SUBSTRING(t.Code,1,1) = 'A'
END

Open in new window

I have provided a generic solution requiring entering the  Code = 'IAAE' once. You could also do this like this:
update t
    set Code = '####'  
    from Woop t
    where t.DateEntered > ( select min(x.DateEntered) from Woop x where x.Code='IAAE')  AND Code = 'IAAE' -- AND SUBSTRING(t.Code,1,1) = 'A'
END

Open in new window

However, if you have many duplicating records you can do a generic query that will take care of all them:
WITH q as(
SELECT *,
ROW_NUMBER() OVER(Partition BY Code Order BY DateEntered) rn
FROM Woop
Where Code <> '####')
Update q Set code = '####' WHERE rn > 1

Open in new window

0
 
LVL 2

Author Closing Comment

by:brgdotnet
ID: 40488187
Thanks, I actually figured it out, but your solution ROCKS
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
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…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

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

20 Experts available now in Live!

Get 1:1 Help Now