greatest-n-per-group

Row with latest value by customer and month [duplicate]

丶灬走出姿态 提交于 2021-01-04 04:21:08
问题 This question already has answers here : Fetch a record on maximum date of every month (3 answers) Closed last month . I have a table that keeps track of changes in customer profiles. Here's a simplified version: CREATE TABLE HISTORY ( CUSTOMER_ID NUMBER(9,0), DATE_CHANGED DATE, ACCOUNT_TYPE VARCHAR2(20), CONSTRAINT HISTORY_PK PRIMARY KEY (CUSTOMER_ID, DATE_CHANGED) ); INSERT INTO HISTORY (CUSTOMER_ID, DATE_CHANGED, ACCOUNT_TYPE) VALUES (200, TO_DATE('05/01/2013 00:00:00','DD/MM/RRRR HH24:MI

Row with latest value by customer and month [duplicate]

五迷三道 提交于 2021-01-04 04:20:39
问题 This question already has answers here : Fetch a record on maximum date of every month (3 answers) Closed last month . I have a table that keeps track of changes in customer profiles. Here's a simplified version: CREATE TABLE HISTORY ( CUSTOMER_ID NUMBER(9,0), DATE_CHANGED DATE, ACCOUNT_TYPE VARCHAR2(20), CONSTRAINT HISTORY_PK PRIMARY KEY (CUSTOMER_ID, DATE_CHANGED) ); INSERT INTO HISTORY (CUSTOMER_ID, DATE_CHANGED, ACCOUNT_TYPE) VALUES (200, TO_DATE('05/01/2013 00:00:00','DD/MM/RRRR HH24:MI

Fetch a record on maximum date of every month

落爺英雄遲暮 提交于 2020-12-15 06:26:16
问题 I want to fetch customers balances at the maximum date of every month, in every year in database. The Balance table has balances at the end of everyday when customer does transaction. I just want to pick the balance at the maximum date of every month.Any help?? Below is a snip of My dataset. 回答1: You can try using window function - row_number() select * from ( SELECT *,row_number() over(partition by extract(YEAR FROM Date), extract(MONTH FROM Date) order by date desc) as rn FROM t )rn=1 回答2:

Fetch a record on maximum date of every month

偶尔善良 提交于 2020-12-15 06:26:07
问题 I want to fetch customers balances at the maximum date of every month, in every year in database. The Balance table has balances at the end of everyday when customer does transaction. I just want to pick the balance at the maximum date of every month.Any help?? Below is a snip of My dataset. 回答1: You can try using window function - row_number() select * from ( SELECT *,row_number() over(partition by extract(YEAR FROM Date), extract(MONTH FROM Date) order by date desc) as rn FROM t )rn=1 回答2: