unpivot-postgres.sql
✓ Published0🌍 Public
Aantichaos
Last edited Jun 11, 2020
Created on Jun 11, 2020
This example reshapes two country-level datasets into a unified, chart-ready format for visualizing trends over time. It first pivots the CyberSecurityIndex table, converting three separate columns—National Cyber Security Index, Digital Development Level, and their difference—into a long-form structure using a `cross join lateral` with a `values` clause. It then appends rows from the PressFreedomIndex table, retaining its country, ISO code, year, and index value. The SQL uses `CAST` to normalize year and value types, ensuring consistent numeric and text formats across both sources before combining them with `UNION`.
AI-generated description