我有一个status
表,我想获取最新信息。
Slno | ID | Status | date
1 | 1 | Pass | 15-06-2015 11:11:00 - this is inserted first
2 | 1 | Fail | 15-06-2015 11:11:00 - this is inserted second
3 | 2 | Fail | 15-06-2015 12:11:11 - this is inserted first
4 | 2 | Pass | 15-06-2015 12:11:11 - this is inserted second
我使用的窗口函数与partition by ID order by date desc
来获取第一值。
除外输出:
2 | 1 | Fail | 15-06-2015 11:11:00 - this is inserted second
4 | 2 | Pass | 15-06-2015 12:11:11 - this is inserted second
实际输出:
1 | 1 | Pass | 15-06-2015 11:11:00 - this is inserted first
3 | 2 | Fail | 15-06-2015 12:11:11 - this is inserted first
根据[ http://docs.aws.amazon.com/redshift/latest/dg/r_Examples_order_by_WF.html] ,添加第二ORDER BY
列的窗函数可以解决该问题。 但是,我没有任何其他的列来区分行!
有没有办法解决这个问题的另一种方法?
编辑:我添加slno
这里清晰。 我没有slno
这样的表!
我的SQL:
with range as (
select id from status where date between 01-06-2015 and 30-06-2015
), latest as (
select status, id, row_number() OVER (PARTITION BY id ORDER BY date DESC) row_num
)
select * from latest where row_num = 1