简体   繁体   English

从3列表中选择所有记录,其中2个特定列中有重复项

[英]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.

相关问题 从具有1个不同列和where条件的表中选择所有列 - Select All Columns From Table With 1 Distinct Column and a Where Condition 从两个表中选择所有列,按表1中的所有列和表2中的特定列分组 - Select all columns from two tables grouping by all columns in table1 and specific column in table2 Select 全部来自一个表,其中 2 列是不同的 - Select all from a table, where 2 columns are Distinct 从表中选择记录,其中两列不存在于另一个表中 - Select records from a table where two columns are not present in another table 如何基于一列的非重复选择所有记录 - How to select all records based on non-duplication of one column 从一个表中选择所有列,从另一个表中选择一个列,其中列等于变量 - Select all columns from one table and one from another where column equals variable 使用WHERE一个特定列“Select * from table”Timeout,但其他列可以正常运行 - Using WHERE one specific column to “Select * from table” Timeout, but other columns can run normally 返回表中的所有列,但仅返回特定列不同的行 - Return all columns from a table but only return rows where a specific column is distinct 从Oracle表中选择单列唯一的记录 - Select records from Oracle table where single column is unique select 列具有特定值的所有表中的表名 - select table name from all tables where column has specific value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM