我是SQL的新手,我确信这更简单。 我希望在桌面上获得最新记录,然后按状态计算最新记录。某些能力按状态和月/年返回计数
would be a bonus.
So table is the following:
ID STATUS TIMESTAMP
1 ACTIVE 04/19/2016
1 PENDING 04/18/2016
2 ACTIVE 04/08/2016
2 PENDING 04/01/2016
3 PENDING 04/07/2016
4 PENDING 12/01/2015
5 CANCELLED 12/30/2015
当我运行查询时,我想要以下内容:
ACTIVE 04/2016 2
PENDING 04/2016 1
CANCELLED 12/2015 1
我可以使用以下内容获取最新记录:
Select id,
status,
date from (
Select TABLE_A,
status,
date,
row_number() over (partition by id order by date Desc) col
From TABLE_A) ps
Where ps.col = 1;
感谢您提前耐心等待。
答案 0 :(得分:0)
你大部分都在那里。其余的只是聚合:
select status, to_char(date, 'YYYY-MM') as yyyymm, count(*)
from (Select TABLE_A, status, date,
row_number() over (partition by id order by date Desc) as seqnum
From TABLE_A
) ps
Where seqnum = 1
group by select status, to_char(date, 'YYYY-MM') ;