[英]Select all records from a 3 column table where there is duplication in 2 specific columns
Let's say I have the following table in SQL Server (tbl1): 假设我在SQL Server(tbl1)中具有下表:
Num Zip Rating
1 75235 100
2 77234 50
3 77234 100
4 77234 100
5 77234 100
6 75235 75
7 76076 25
8 76076 25
9 76076 50
10 75234 25
11 75234 50
12 75234 50
I want to select all of the rows where the Zip and Rating columns combined have duplicates. 我想选择Zip和Rating列相结合的所有重复行。 In this case, that would be rows 3, 4, 5, 7, 8, 11, and 12. How would I do that? 在这种情况下,应该是第3、4、5、7、8、11和12行。我该怎么做?
SELECT *
FROM tbl1 t1
WHERE (
SELECT COUNT (*)
FROM tbl1 t2
WHERE t2.Zip = t1.Zip AND t2.Rating = t1.Rating
) > 1
Find all rows that have at least one exact duplicate with all equal values, except for the Num
column which should be different: 查找具有至少一个完全相同且值均相同的重复项的所有行,但Num
列应有所不同:
SELECT *
FROM tbl1 T1
WHERE EXISTS (
SELECT 1
FROM tbl1 T2
WHERE T2.Zip = T1.Zip
AND T2.Rating = T1.Rating
AND T2.Num <> T1.Num
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.