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 |
christosds
Starting Member
10 Posts |
Posted - 2012-07-27 : 06:11:59
|
Helloi have a table like thisCCCORCODE|| MTRL || CODE || COMMENTS=================================================================== 1037 || 15597 || 441906091A || NULL1038 || 15597 || 441906091B || NULL1039 || 15597 || 443906087BB || NULL1040 || 15597 || 443906087BD || NULL1041 || 15597 || 443906091 || NULL1042 || 15597 || 443906091A || NULL1043 || 15597 || 443906091E || NULL1044 || 15597 || 443906087BB || NULL1045 || 15597 || 447906091A || NULL1046 || 15597 || 443906087BB || NULLIs it possible to delete all the double rows when MTRL & CODE are identical and keep only one row:1039 15597 443906087BB NULL (keep only one row)1044 15597 443906087BB NULL1046 15597 443906087BB NULLAny help will be greatly appreciated!? |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-07-27 : 06:22:11
|
[code]DELETE DFROM( SELECT *, RN = row_number() over (partition by MTRL, CODE order by CCCORCODE) FROM yourtable) DWHERE D.RN <> 1[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-07-27 : 06:24:32
|
You can use the row_number function to do this:;WITH cte AS( SELECT ROW_NUMBER() OVER (PARTITION BY MTRL,CODE ORDER BY (SELECT NULL)) AS RN FROM YourTable)DELETE FROM cte WHERE RN > 1; The columns in the PARTITION clause are the columns which you want to delete duplicates of. If you have some ordering scheme that you can use from among the duplicates to decide which one to keep, instead of (SELECT NULL), insert the columns that you would use to order the duplicate rows. The first one in the ordering scheme will be kept. |
 |
|
christosds
Starting Member
10 Posts |
Posted - 2012-07-27 : 07:21:53
|
Guys thanks for the prompt reply! |
 |
|
|
|
|