Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
ttran
Starting Member
23 Posts |
Posted - 2013-05-23 : 19:04:19
|
I've struggled... trying to delete almost duplicate (not exactly duplicate) rows in a table.table_A:ID CD SQ ESD EED1 r 1 aaa bbb2 r 1 ccc ddd2 r 2 ccc ddd3 f 4 aaa bbb3 f 5 aaa bbb3 f 6 aaa bbb4 r 2 ccc ddd5 f 6 aaa bbb5 f 7 aaa bbbAs you can see... the ID 2 are not exactly duplicated because the SQ column are different. But the SQ column is not important to database.I want to keep only highest SQ if the rest of the columns are the same. As being said, the rows should be kept:ID CD SQ ESD EED1 r 1 aaa bbb2 r 2 ccc ddd3 f 6 aaa bbb4 r 2 ccc ddd5 f 7 aaa bbbAnd this is what I already did:(1) I created a temporary table_B (will be deleted after the process is done) contains all the duplicate rows:select id,cd,esd,eed,count(*)as DupCount,max(sq) as maxsq into table_B from table_A group by id,cd,esd,eed having count(*) > 1(2) Then, I tried to delete the rows from table_A which have the sq != maxsq. But before deleting these rows from table_A, I list them for verification:select a.*from table_A a join table_B b on a.id = b.id and a.cd = b.cd and a.esd = b.esd and a.eed = b.eedwhere a.sq != b.maxsqTo my surprise... I got nothing listed.I don't know what I did wrong.Any help or suggestion would be greatly appreciated.Thank you. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-24 : 00:36:53
|
you just need thisDELETE tFROM (SELECT ROW_NUMBER() OVER (PARTITION BY ID, CD, ESD, EED ORDER BY SQ DESC) AS RnFROM YourTable)tWHERE Rn >1 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
ttran
Starting Member
23 Posts |
Posted - 2013-05-24 : 11:56:33
|
It is simple and work well.Thank you...thank you so much, Visakhm16. |
|
|
ttran
Starting Member
23 Posts |
Posted - 2013-05-24 : 12:13:19
|
visakhm16,Your query is perfect. But could you tell me why my 2nd query doesn't work? I just want to learn and avoid mistakes in the future.Thanks again. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-25 : 09:53:48
|
quote: Originally posted by ttran It is simple and work well.Thank you...thank you so much, Visakhm16.
you're welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-25 : 10:03:45
|
i cant find any obvious issues with your query 2------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|