当唯一的区别是增量日期字段时,如何删除Redshift中的重复字段?

时间:2017-09-23 21:06:13

标签: sql amazon-redshift

参见示例表,其中日期1/3到1/5的行的唯一差异是日期字段。

Date, ID, State, Amount
1/1/2017, 123, A, 1
1/2/2017, 123, B, 1
1/3/2017, 123, C, 1
1/4/2017, 123, C, 1
1/5/2017, 123, C, 1
1/6/2017, 345, B, 1

如何删除列ID,状态,金额的重复行,所以它看起来像这样?

Date, ID, State, Amount
1/1/2017, 123, A, 1
1/2/2017, 123, B, 1
1/3/2017, 123, C, 1
1/6/2017, 345, B, 1

2 个答案:

答案 0 :(得分:0)

select    min(date) as date, id, state, amount
from      some_table_name_you_never_disclosed
group by  id, state, amount;

BTW,date是一个SQL92保留关键字,因此您可能想要更改该列名。

答案 1 :(得分:0)

您可以创建一个临时表,并将要删除的重复记录存储为

CREATE TEMPORARY TABLE temp
AS
SELECT DATE,
       ID
FROM (SELECT DATE,
             ID,
             ROW_NUMBER() OVER (PARTITION BY ID,State,Amount ORDER BY DATE ASC) AS rnk
      FROM your_table_name)
WHERE rnk > 1

然后使用delete statement作为

DELETE
FROM your_table_name USING temp
WHERE your_table_name.date = temp.date
AND   your_table_name.id = temp.id