Skip to content

Instantly share code, notes, and snippets.

@antichaos
Created June 11, 2020 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antichaos/f1c4f5fa2e3056003a435451ccdcfdfb to your computer and use it in GitHub Desktop.
Save antichaos/f1c4f5fa2e3056003a435451ccdcfdfb to your computer and use it in GitHub Desktop.
unpivot-postgres.sql
SELECT
'CyberSecurityIndex' AS "index",
"CyberSecurityIndex"."Country" AS "Country",
"CyberSecurityIndex"."iso3" AS "iso3",
CAST("CyberSecurityIndex"."Year" AS TEXT) AS "Year",
t.subindex AS subindex,
CAST(t.value AS float) AS value
FROM "indices"."009_cybersecurityindex" "CyberSecurityIndex"
cross join lateral (
values
("CyberSecurityIndex"."National Cyber Security Index", 'National Cyber Security Index'),
("CyberSecurityIndex"."Digital Development Level", 'Digital Development Level'),
("CyberSecurityIndex"."Difference", 'Difference NCSI-DPL')
) as t(value, subindex)
UNION
SELECT
'PressfreeomdIndex' AS "index",
"pressfreedomindex"."countryname" as "Country",
"pressfreedomindex"."iso3" as "iso3",
CAST("pressfreedomindex"."year" AS TEXT) AS "Year",
NULL AS "subindex",
CAST(indexvalue AS float) AS value
FROM "indices"."001_pressfreedomindex" "pressfreedomindex"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment