首页 > 解决方案 > SQL/Postgres:查找具有相同值的元组(对于另一列中的给定值)

问题描述

我的表 ANSWERS_T 是

id    answer_date_val             event_id
..    ...                         ...
103   2019-10-18 09:30:00.000     145
104   2019-10-18 09:35:00.000     145
105   2019-10-18 10:45:00.000     146
106   2019-10-18 10:50:00.000     146
..

我的查询需要找到 ANSWERS_T 行,其中同一 event_id 有多个相同的 answer_date_val

这是在 Postgres 中。

标签: sqlpostgresql

解决方案


如果您需要 answer_date_val 和 event_id 的重复条目,您可以尝试使用子查询来重复连接到基表

  select * 
  from ANSWERS_T a 
  inner join  (
      select  event_id, answer_date_val
      from  ANSWERS_T
      group  by event_id, answer_date_val
      having count(*) >= 2 
  ) t on t.event_id= a.event_id
      and t.answer_date_val = a.answer_date_val

推荐阅读