简体   繁体   中英

Compare two rows in the same table for equality in SQL

I have the following table in my database:

Table name: 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.

For the table above I should get the following result set:

1 | 34564

3 | 34564
4 | 98271
6 | 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
);

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