busi488energy/prisma/sql/getDemandDaily.sql
Joey Eamigh 8f99f6535e
phase 7: full US coverage — grid regions, datacenters, power plants, backfill, chart perf
- Add 7 new grid regions (BPA, DUKE, SOCO, TVA, FPC, WAPA, NWMT) to cover entire continental US
- Expand datacenters from 108 to 292 facilities across 39 operators
- Add EIA power plant pipeline: download script, 3,546 plants >= 50 MW with diamond map markers
- Rewrite backfill script for 10-year data (2015-07-01) with quarterly/monthly chunking, 3-region parallelism, resumability
- Add materialized views (daily/weekly) with server-side granularity selection for chart performance
- Fix map UX: z-index tooltips, disable POI clicks, move legend via MapControl
2026-02-11 16:08:06 -05:00

22 lines
709 B
SQL

-- @param {DateTime} $1:startDate
-- @param {DateTime} $2:endDate
-- @param {String} $3:regionCode - pass 'ALL' to return all regions
SELECT
r.code AS region_code,
r.name AS region_name,
d.day,
d.avg_demand,
d.peak_demand,
COALESCE(dc.datacenter_count, 0)::INT AS datacenter_count,
COALESCE(dc.total_dc_capacity_mw, 0) AS total_dc_capacity_mw
FROM electricity_prices_daily d
JOIN grid_regions r ON d.region_id = r.id
LEFT JOIN (
SELECT region_id, COUNT(*)::INT AS datacenter_count,
COALESCE(SUM(capacity_mw), 0) AS total_dc_capacity_mw
FROM datacenters GROUP BY region_id
) dc ON dc.region_id = r.id
WHERE d.day BETWEEN $1 AND $2
AND ($3 = 'ALL' OR r.code = $3)
ORDER BY r.code, d.day