Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DROP TABLE IF EXISTS test.test_data;
- CREATE TABLE test.test_data(
- id_user INT2,
- thn INT2,
- bln INT2,
- tgl INT2,
- nilai INT
- );
- DELETE FROM test.test_data;
- INSERT INTO test.test_data(id_user,thn,bln,tgl,nilai)
- VALUES
- (1,2022,10,30,12),
- (1,2022,10,31,15),
- (1,2022,11,1,1),
- (1,2022,11,2,1),
- (1,2022,11,3,1),
- (1,2022,11,4,1),
- (1,2022,11,5,1),
- (1,2022,11,6,2),
- (1,2022,11,7,2),
- (1,2022,11,8,2),
- (1,2022,11,9,2),
- (1,2022,11,10,2),
- (1,2022,11,11,2),
- (1,2022,11,12,4),
- (1,2022,11,13,4),
- (1,2022,11,14,6),
- (1,2022,11,15,7),
- (1,2022,11,16,9),
- (1,2022,11,17,9),
- (1,2022,11,18,9),
- (1,2022,11,19,11),
- (1,2022,11,20,14),
- (1,2022,11,21,14),
- (1,2022,11,22,14),
- (1,2022,11,23,15),
- (1,2022,11,24,16),
- (1,2022,11,25,17),
- (1,2022,11,26,17),
- (1,2022,11,27,18),
- (1,2022,11,28,20),
- (1,2022,11,29,23),
- (1,2022,11,30,24),
- (1,2022,12,1,1),
- (1,2022,12,2,1),
- (1,2022,12,3,2),
- (1,2022,12,4,3),
- (4,2022,10,30,10),
- (4,2022,10,31,12),
- (4,2022,11,1,1),
- (4,2022,11,2,2),
- (4,2022,11,3,2),
- (4,2022,11,4,3),
- (4,2022,11,5,3),
- (4,2022,11,6,4),
- (4,2022,11,7,5),
- (4,2022,11,8,7),
- (4,2022,11,9,7),
- (4,2022,11,10,8),
- (4,2022,11,11,9),
- (4,2022,11,12,10),
- (4,2022,11,13,14),
- (4,2022,11,14,16),
- (4,2022,11,15,17),
- (4,2022,11,16,19),
- (4,2022,11,17,19),
- (4,2022,11,18,19),
- (4,2022,11,19,21),
- (4,2022,11,20,22),
- (4,2022,11,21,22),
- (4,2022,11,22,23),
- (4,2022,11,23,24),
- (4,2022,11,24,25),
- (4,2022,11,25,26),
- (4,2022,11,26,27),
- (4,2022,11,27,27),
- (4,2022,11,28,28),
- (4,2022,11,29,28),
- (4,2022,11,30,29),
- (4,2022,12,1,1),
- (4,2022,12,2,2),
- (4,2022,12,3,4),
- (4,2022,12,4,5);
- SELECT
- x.id_user,x.thn,x.bln,x.tgl,nilai
- FROM
- (
- SELECT
- id_user,thn,bln,tgl,nilai,
- RANK() OVER(
- PARTITION BY id_user,thn,bln
- ORDER BY thn DESC, bln DESC, nilai DESC
- ) AS ranking
- FROM test.test_data
- ) x
- WHERE
- x.ranking=1
- ORDER BY
- x.id_user,x.thn,x.bln,x.tgl
- ;
- id_user | thn | bln | tgl | nilai
- 1 | 2022 | 10 | 31 | 15
- 1 | 2022 | 11 | 30 | 24
- 1 | 2022 | 12 | 4 | 3
- 4 | 2022 | 10 | 31 | 12
- 4 | 2022 | 11 | 30 | 29
- 4 | 2022 | 12 | 4 | 5
- WITH x AS (
- SELECT
- id_user,thn,bln,tgl,nilai,
- RANK() OVER(
- PARTITION BY id_user,thn,bln
- ORDER BY thn DESC, bln DESC, nilai DESC
- ) AS ranking
- FROM test.test_data
- )
- SELECT
- x.id_user,x.thn,x.bln,x.tgl,nilai
- FROM x
- WHERE
- x.ranking=1
- ORDER BY
- x.id_user,x.thn,x.bln,x.tgl
- ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement