我需要删除仅在特定列中具有相同值的行。例如,在下面的摘录中,我想选择除最后一行之外的所有行,它们等于CODE,START_DATE和TYPE列的倒数第二行(这意味着忽略END_DATE列的值)。
code | start_date | end_date | type
---------------+----------------+--------------+------
C086000-T10001 | 2014-11-11 | 2014-11-12 | 01
C086000-T10001 | 2014-11-11 | 2014-11-11 | 03
C086000-T10002 | 2014-12-03 | 2014-12-10 | 03
C086000-T10002 | 2014-01-03 | 2014-01-04 | 03
C086000-T10003 | 2012-02-27 | 2014-02-28 | 03
C086000-T10003 | 2014-08-11 | 2014-11-12 | 01
C086000-T10003 | 2014-08-11 | 2014-08-20 | 01
我怎么能这样做?
编辑:以下查询为子查询错误消息返回列太多:
SELECT * FROM my_table WHERE code NOT IN (SELECT DISTINCT code, start_date, type FROM my_table) ;
非常感谢您的帮助!
答案 0 :(得分:1)
这可以使用Postgres' distinct on
运营商:
select distinct on (code, start_date, type) code, start_date, end_date, type
from the_table
order by code, start_date, type;
如果您更喜欢使用标准SQL,也可以使用窗口函数来完成:
select code, start_date, end_date, type
from (
select code, start_date, end_date, type,
row_number() over (partition by code, start_date, type order by end_date) as rn
from the_table
) t
where rn = 1
order by code, start_date, type;
SQLFiddle示例:http://sqlfiddle.com/#!15/c5044/1