lesson 44 Window functions · advanced
Share of total with windows
Compare each row with its group total.
the idea
SUM(x) OVER () is the total of x over the whole result; dividing each row by it gives its share of the total. SUM(x) OVER (PARTITION BY group) gives each group’s total for per-group shares. Aggregate to the right grain first, then compute shares.
Dataset: Energy
The tables the challenges use, with their first 6 rows. Under each column name: the kind of value it holds, and which table it links to.
generation_by_fuel5 columns · 15,824 rows
| ba_codetext · links to balancing_authorities | datedate · links to calendar | fuel_codetext · links to fuel_types | generation_mwhnumber | hours_reportednumber |
|---|---|---|---|---|
| BPAT | 2024-01-01 | NG | 15971 | 24 |
| BPAT | 2024-01-01 | NUC | 27693 | 24 |
| BPAT | 2024-01-01 | OTH | 4387 | 24 |
| BPAT | 2024-01-01 | SUN | 148 | 24 |
| BPAT | 2024-01-01 | WAT | 162900 | 24 |
| BPAT | 2024-01-01 | WND | 306 | 24 |
- ba_codetext · links to balancing_authorities
- BPAT
- datedate · links to calendar
- 2024-01-01
- fuel_codetext · links to fuel_types
- NG
- generation_mwhnumber
- 15971
- hours_reportednumber
- 24
- ba_codetext · links to balancing_authorities
- BPAT
- datedate · links to calendar
- 2024-01-01
- fuel_codetext · links to fuel_types
- NUC
- generation_mwhnumber
- 27693
- hours_reportednumber
- 24
- ba_codetext · links to balancing_authorities
- BPAT
- datedate · links to calendar
- 2024-01-01
- fuel_codetext · links to fuel_types
- OTH
- generation_mwhnumber
- 4387
- hours_reportednumber
- 24
- ba_codetext · links to balancing_authorities
- BPAT
- datedate · links to calendar
- 2024-01-01
- fuel_codetext · links to fuel_types
- SUN
- generation_mwhnumber
- 148
- hours_reportednumber
- 24
- ba_codetext · links to balancing_authorities
- BPAT
- datedate · links to calendar
- 2024-01-01
- fuel_codetext · links to fuel_types
- WAT
- generation_mwhnumber
- 162900
- hours_reportednumber
- 24
- ba_codetext · links to balancing_authorities
- BPAT
- datedate · links to calendar
- 2024-01-01
- fuel_codetext · links to fuel_types
- WND
- generation_mwhnumber
- 306
- hours_reportednumber
- 24
first 6 of 15,824 rows · your queries use all of them
worked example
Run these steps in order to see the idea before you write your own query. The step marked “the pattern” is what the challenges ask for. Running the example does not count toward progress.
See raw generation rows. Run this. Each line is one balancing authority and fuel type on 2024-04-15 before you roll up or compute shares.
step-1.sqlSELECT ba_code, fuel_code, generation_mwh, hours_reported FROM generation_by_fuel WHERE date = '2024-04-15' ORDER BY ba_code, fuel_code LIMIT 20;
Turn totals into percentages. Run this. SUM() OVER () with empty parentheses is the day’s total across all authorities; dividing each group’s sum by that gives its share of generation.the pattern
example.sqlSELECT ba_code, SUM(generation_mwh) AS mwh, ROUND(100.0 * SUM(generation_mwh) / SUM(SUM(generation_mwh)) OVER (), 2) AS share_pct FROM generation_by_fuel WHERE date = '2024-01-15' GROUP BY ba_code;
common mistake
Pumped storage can make a daily fuel total negative, so a share can be negative; a zero total needs NULLIF to avoid dividing by zero.