简体   繁体   English

比较同一个表中的两行是否在SQL中相等

[英]Compare two rows in the same table for equality in SQL

I have the following table in my database: 我的数据库中有以下表格:

Table name: INSURANCE TABLE 表名:INSURANCE TABLE

ID | Policy 
1  | 34564  
2  | 67548  
3  | 34564  
4  | 98271  
5  | 90198  
6  | 98271  

I am looking for a sql query that will compare the Policy column values in all 5 rows and return those rows which have a value equal to atleast one other row. 我正在寻找一个SQL查询,它将比较所有5行中的Policy列值,并返回那些值等于至少另一行的行。

For the table above I should get the following result set: 对于上表,我应该得到以下结果集:

1 | 1 | 34564 34564

3 | 3 | 34564 34564
4 | 4 | 98271 98271
6 | 6 | 98271 98271

I would appreciate responses on how to write this query. 我很感激如何编写此查询的回复。

If I understand you, you want to group by Policy, discarding unique values: 如果我了解您,您希望按策略分组,丢弃唯一值:

select * from your_table where Policy in (
    select Policy from your_table group by Policy having count(*) > 1
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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