Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. CREATE MATERIALIZED VIEW clrtap_nec_unfccc.running_total_and_target (
  2. year,
  3. country_code,
  4. pollutant_name,
  5. sector_code,
  6. emissions,
  7. target,
  8. running_total)
  9. AS
  10. SELECT source.year,
  11. source.country_code,
  12. source.pollutant_name,
  13. source.sector_code,
  14. source.emissions,
  15. target.target,
  16. source.running_total
  17. FROM (
  18. SELECT clrtap_nfr09_gf.year,
  19. clrtap_nfr09_gf.country_code,
  20. clrtap_nfr09_gf.pollutant_name,
  21. clrtap_nfr09_gf.emissions,
  22. clrtap_nfr09_gf.sector_code,
  23. sum(clrtap_nfr09_gf.emissions) OVER (PARTITION BY
  24. clrtap_nfr09_gf.year, clrtap_nfr09_gf.country_code, clrtap_nfr09_gf.pollutant_name
  25. ORDER BY clrtap_nfr09_gf.emissions DESC) AS running_total
  26. FROM clrtap_nec_unfccc.clrtap_nfr09_gf
  27. WHERE clrtap_nfr09_gf.sector_code::text !~~ 'NATIONAL%'::text AND
  28. clrtap_nfr09_gf.sector_code::text !~~ 'ADJ%'::text AND clrtap_nfr09_gf.emissions > 0::double precision
  29. ORDER BY clrtap_nfr09_gf.emissions DESC
  30. ) source,
  31. (
  32. SELECT clrtap_nfr09_gf.year,
  33. clrtap_nfr09_gf.country_code,
  34. clrtap_nfr09_gf.pollutant_name,
  35. sum(clrtap_nfr09_gf.emissions) * 0.8::double precision AS target,
  36. sum(clrtap_nfr09_gf.emissions) AS total
  37. FROM clrtap_nec_unfccc.clrtap_nfr09_gf
  38. WHERE clrtap_nfr09_gf.sector_code::text !~~ 'NATIONAL%'::text AND
  39. clrtap_nfr09_gf.sector_code::text !~~ 'ADJ%'::text AND clrtap_nfr09_gf.emissions > 0::double precision
  40. GROUP BY clrtap_nfr09_gf.year, clrtap_nfr09_gf.country_code,
  41. clrtap_nfr09_gf.pollutant_name
  42. ORDER BY clrtap_nfr09_gf.year, clrtap_nfr09_gf.country_code,
  43. clrtap_nfr09_gf.pollutant_name
  44. ) target
  45. WHERE source.year::text = target.year::text AND source.country_code::text =
  46. target.country_code::text AND source.pollutant_name::text = target.pollutant_name::text
  47. ORDER BY source.year, source.country_code, source.pollutant_name, source.emissions DESC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement