By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE
INSERT 0 26
| QUERY PLAN |
|---|
| Sort (cost=309.83..310.33 rows=200 width=36) |
| Sort Key: a.record_date |
| -> HashAggregate (cost=300.18..302.18 rows=200 width=36) |
| Group Key: a.record_date, a.country |
| -> HashAggregate (cost=265.26..277.96 rows=1270 width=36) |
| Group Key: a.record_date, a.country, b.objectid |
| -> Merge Left Join (cost=176.34..252.56 rows=1270 width=36) |
| Merge Cond: (((a.country)::text = (b.country)::text) AND ((date_part('month'::text, (a.record_date)::timestamp without time zone)) = (date_part('month'::text, (b.record_date)::timestamp without time zone))) AND ((date_part('year'::text, (a.record_date)::timestamp without time zone)) = (date_part('year'::text, (b.record_date)::timestamp without time zone)))) |
| Join Filter: (a.record_date >= b.record_date) |
| -> Sort (cost=88.17..91.35 rows=1270 width=28) |
| Sort Key: a.country, (date_part('month'::text, (a.record_date)::timestamp without time zone)), (date_part('year'::text, (a.record_date)::timestamp without time zone)) |
| -> Seq Scan on mytable a (cost=0.00..22.70 rows=1270 width=28) |
| -> Sort (cost=88.17..91.35 rows=1270 width=36) |
| Sort Key: b.country, (date_part('month'::text, (b.record_date)::timestamp without time zone)), (date_part('year'::text, (b.record_date)::timestamp without time zone)) |
| -> Seq Scan on mytable b (cost=0.00..22.70 rows=1270 width=36) |
EXPLAIN
| QUERY PLAN |
|---|
| GroupAggregate (cost=49180.02..49194.72 rows=200 width=36) |
| Group Key: d.record_date, d.country |
| -> Sort (cost=49180.02..49183.20 rows=1270 width=36) |
| Sort Key: d.record_date, d.country |
| -> Nested Loop (cost=38.63..49114.55 rows=1270 width=36) |
| -> Seq Scan on mytable d (cost=0.00..22.70 rows=1270 width=28) |
| -> Unique (cost=38.63..38.63 rows=1 width=12) |
| -> Sort (cost=38.63..38.63 rows=1 width=12) |
| Sort Key: (sum((max(a.objectuse))) OVER (?)) |
| -> WindowAgg (cost=38.59..38.62 rows=1 width=12) |
| -> GroupAggregate (cost=38.59..38.60 rows=1 width=8) |
| Group Key: a.objectid |
| -> Sort (cost=38.59..38.59 rows=1 width=8) |
| Sort Key: a.objectid |
| -> Seq Scan on mytable a (cost=0.00..38.58 rows=1 width=8) |
| Filter: ((record_date <= d.record_date) AND ((country)::text = (d.country)::text) AND (record_date >= date_trunc('month'::text, (d.record_date)::timestamp with time zone))) |
EXPLAIN
| record_date | country | usetotal |
|---|---|---|
| 2022-07-01 | A | 8 |
| 2022-07-01 | chile | 12 |
| 2022-07-02 | A | 8 |
| 2022-07-02 | chile | 12 |
| 2022-07-03 | A | 10 |
| 2022-07-03 | chile | 15 |
| 2022-07-04 | A | 10 |
| 2022-07-04 | chile | 15 |
| 2022-07-20 | chile | 19 |
| 2022-07-26 | peru | 4 |
| 2022-07-27 | peru | 4 |
| 2022-07-28 | peru | 4 |
| 2022-07-30 | peru | 4 |
| 2022-07-31 | peru | 4 |
SELECT 14