Solved

I need help with my update statement

Posted on 2014-12-08
2
158 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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.

770 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