?
Solved

I need help with my update statement

Posted on 2014-12-08
2
Medium Priority
?
175 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
[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
2 Comments
 
LVL 25

Accepted Solution

by:
chaau earned 2000 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

771 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