Solved

I need help with my update statement

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

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

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

730 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