简体   繁体   中英

How to filter duplicates in MS SQL Server Management Studio?

I have some the columns A to D and would like to filter duplicates. We have a duplicate if column A and B have the same value. The returned value in columns C and D should be any value that is stated in one of the aggregated lines.

I tried to fix it with "Group By" but I don't know what to do with the columns C and D. I do not need a aggregated value and it is possible that there is test in the corresponding fields.

EDIT

Sample data:

Starting point:

A B C D
1 2 4 1
1 2 3 2
2 2 4 1
2 2 3 1
1 2 1 2

expected result:

A B C D
1 2 4 1
2 2 4 1

where the columns C and D could be any of the possible values.

I tried:

SELECT T1.A, T1.B, T1.C, T1.D
From DB.T1
Grouped By A, B

but this does not work.

Little hard to tell from what you said but you probably want something like this:

 SELECT *
 FROM (
   SELECT T1.*,
          ROW_NUMBER() OVER (PARTITION BY A, B ORDER BY C, D) AS RN
   FROM T1

 ) X
 WHERE RN = 1

I hope this help you for your problem :

Your solution :

select A ,B ,C ,D from test_table group by A ,B

You can try this for problem :

select A ,B ,max(C) ,max(D) from test_table --where C = 4 or C = 3 or D = ... etc group by A ,B

sqlFiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM