Postgresql - How to get value from last record of each month
|
year |
month |
category |
avg_val |
max_val_of_month |
1 |
2017 |
1 |
A |
5,5000 |
10 |
2 |
2017 |
1 |
B |
6,5000 |
11 |
3 |
2017 |
1 |
C |
7,5000 |
12 |
4 |
2017 |
2 |
A |
5,5000 |
10 |
5 |
2017 |
2 |
B |
6,5000 |
11 |
6 |
2017 |
2 |
C |
7,5000 |
12 |
|
QUERY PLAN |
1 |
Subquery Scan on w (cost=319.24..404.74 rows=11 width=104) (actual time=11.150..13.475 rows=570 loops=1) |
2 |
Filter: (w.rn = 1) |
3 |
Rows Removed by Filter: 1710 |
4 |
-> WindowAgg (cost=319.24..376.24 rows=2280 width=116) (actual time=11.130..13.155 rows=2280 loops=1) |
5 |
-> Sort (cost=319.24..324.94 rows=2280 width=108) (actual time=11.110..11.362 rows=2280 loops=1) |
6 |
Sort Key: table_name.year, table_name.month, table_name.category, table_name.week DESC |
7 |
Sort Method: quicksort Memory: 275kB |
8 |
-> WindowAgg (cost=0.28..192.08 rows=2280 width=108) (actual time=0.117..9.162 rows=2280 loops=1) |
9 |
-> Index Scan using idx_table_name_year_month_category_week_desc on table_name (cost=0.28..146.48 rows=2280 width=76) (actual time=0.067..4.106 rows=2280 loops=1) |
10 |
Planning time: 0.467 ms |
11 |
Execution time: 13.816 ms |
|
QUERY PLAN |
1 |
Unique (cost=319.24..342.04 rows=228 width=108) (actual time=8.958..9.695 rows=570 loops=1) |
2 |
-> Sort (cost=319.24..324.94 rows=2280 width=108) (actual time=8.956..9.153 rows=2280 loops=1) |
3 |
Sort Key: year, month, category, week DESC |
4 |
Sort Method: quicksort Memory: 275kB |
5 |
-> WindowAgg (cost=0.28..192.08 rows=2280 width=108) (actual time=0.054..7.306 rows=2280 loops=1) |
6 |
-> Index Scan using idx_table_name_year_month_category_week_desc on table_name (cost=0.28..146.48 rows=2280 width=76) (actual time=0.023..2.636 rows=2280 loops=1) |
7 |
Planning time: 0.237 ms |
8 |
Execution time: 9.933 ms |
|
QUERY PLAN |
1 |
GroupAggregate (cost=19142.26..19179.31 rows=228 width=104) (actual time=16.565..18.261 rows=570 loops=1) |
2 |
Group Key: t.year, t.month, t.category, table_name.value |
3 |
-> Sort (cost=19142.26..19147.96 rows=2280 width=104) (actual time=16.541..16.744 rows=2280 loops=1) |
4 |
Sort Key: t.year, t.month, t.category, table_name.value |
5 |
Sort Method: quicksort Memory: 275kB |
6 |
-> Nested Loop (cost=0.28..19015.10 rows=2280 width=104) (actual time=0.036..13.678 rows=2280 loops=1) |
7 |
-> Seq Scan on table_name t (cost=0.00..39.80 rows=2280 width=72) (actual time=0.020..0.560 rows=2280 loops=1) |
8 |
-> Limit (cost=0.28..8.30 rows=1 width=36) (actual time=0.005..0.005 rows=1 loops=2280) |
9 |
-> Index Scan using idx_table_name_year_month_category_week_desc on table_name (cost=0.28..8.30 rows=1 width=36) (actual time=0.004..0.004 rows=1 loops=2280) |
10 |
Index Cond: ((year = t.year) AND (month = t.month) AND (category = t.category)) |
11 |
Planning time: 0.397 ms |
12 |
Execution time: 18.547 ms |
|
QUERY PLAN |
1 |
GroupAggregate (cost=0.28..178.40 rows=228 width=104) (actual time=0.050..9.144 rows=570 loops=1) |
2 |
Group Key: year, month, category |
3 |
-> Index Scan using idx_table_name_year_month_category_week_desc on table_name (cost=0.28..146.48 rows=2280 width=76) (actual time=0.013..2.534 rows=2280 loops=1) |
4 |
Planning time: 0.213 ms |
5 |
Execution time: 9.290 ms |
|
QUERY PLAN |
1 |
GroupAggregate (cost=0.28..742.13 rows=228 width=104) (actual time=0.111..12.297 rows=570 loops=1) |
2 |
Group Key: year, month, category |
3 |
-> Index Scan using idx_table_name_year_month_category_week_desc on table_name (cost=0.28..146.48 rows=2280 width=76) (actual time=0.012..2.167 rows=2280 loops=1) |
4 |
Planning time: 0.159 ms |
5 |
Execution time: 12.400 ms |
|
λ
.NET NoSQL database for rapid development
|