如何获得所有值都与唯一ID相同的那些行

时间:2018-06-24 17:42:50

标签: amazon-redshift

我有下面提到的表格:

ID       State      City       Pincode      Code      Date
U-1      AAB        CCV        141414       121       2018-04-04 18:08:17
U-1      AAB        CCV        141414       121       2018-04-04 18:08:17
U-2      BTB        ERV        150454       145       2018-05-05 19:11:25
U-2      BTB        ERV        150454       145       2018-05-05 19:11:25
U-3      FFT        ERT        160707       150       2018-05-22 21:37:45
U-4      FFT        RTT        160707       150       2018-05-28 14:23:48

我只想获取在特定唯一ID中所有值都相同的那些行。

输出:

ID       State      City       Pincode      Code      Date
U-1      AAB        CCV        141414       121       2018-04-04 18:08:17
U-1      AAB        CCV        141414       121       2018-04-04 18:08:17
U-2      BTB        ERV        150454       145       2018-05-05 19:11:25
U-2      BTB        ERV        150454       145       2018-05-05 19:11:25

2 个答案:

答案 0 :(得分:2)

获取重复的行并将结果连接到原始表。

select * from table a
join ( select id,state,city,pincode,code,date
       from table 
       group by id,state,city,pincode,code,date 
       having count(*) > 1 ) b
on a.id = b.id
and a.state = b.state
and a.city = b.city
and a.pincode = b.pincode
and a.code = b.code
and a.date=b.date

答案 1 :(得分:0)

您可以尝试以下方法:

SELECT * FROM table WHERE ID IN (
    SELECT count(*) AS c FROM table 
    WHERE c > 1 
    GROUP BY ID
)

获取具有该ID的记录数大于2的所有行(具有该ID的至少两行)