I am a novice with SQL and sure this is much more simple I can think of. I am looking to get the most recent record on a table and then count the most recent record by status. Some ability return a count by status and month/year
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
When I run the query I am wanting the following:
ACTIVE 04/2016 2
PENDING 04/2016 1
CANCELLED 12/2015 1
I am able to get the most recent record by using the following:
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;
Thank you for your patience in advance.
You are most of the way there. The rest is just aggregation:
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') ;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.