runtime
44 / 88

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_authoritiesdatedate · links to calendarfuel_codetext · links to fuel_typesgeneration_mwhnumberhours_reportednumber
BPAT2024-01-01NG1597124
BPAT2024-01-01NUC2769324
BPAT2024-01-01OTH438724
BPAT2024-01-01SUN14824
BPAT2024-01-01WAT16290024
BPAT2024-01-01WND30624
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.

  1. 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.sql
    SELECT 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;
  2. 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.sql
    SELECT
      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.

challenge 1

For 2024-04-15, return fuel_code, generation_mwh (summed over all balancing authorities) and share_pct: that fuel’s share of the day’s total generation, 0–100, rounded to 2 decimals.

what to return

Columns
fuel_codegeneration_mwhshare_pct in this order
Rows
any order
challenge-1.sql
 
To master this lesson, solve both challenges without hints and pass the concept check. 3 of 3 to go.
Share of total with windows — Window functions · Runtime