q08.sql 822 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. --!syntax_pg
  2. --TPC-H Q8
  3. select
  4. o_year,
  5. sum(case
  6. when nation = 'BRAZIL'
  7. then volume
  8. else 0::numeric
  9. end) / (sum(volume) + 1e-12::numeric) as mkt_share
  10. from (
  11. select
  12. extract(year from o_orderdate) as o_year,
  13. l_extendedprice * (1::numeric-l_discount) as volume,
  14. n2.n_name as nation
  15. from
  16. plato."part",
  17. plato."supplier",
  18. plato."lineitem",
  19. plato."orders",
  20. plato."customer",
  21. plato."nation" n1,
  22. plato."nation" n2,
  23. plato."region"
  24. where
  25. p_partkey = l_partkey
  26. and s_suppkey = l_suppkey
  27. and l_orderkey = o_orderkey
  28. and o_custkey = c_custkey
  29. and c_nationkey = n1.n_nationkey
  30. and n1.n_regionkey = r_regionkey
  31. and r_name = 'AMERICA'
  32. and s_nationkey = n2.n_nationkey
  33. and o_orderdate between date '1995-01-01' and date '1996-12-31'
  34. and p_type = 'ECONOMY ANODIZED STEEL'
  35. ) as all_nations
  36. group by
  37. o_year
  38. order by
  39. o_year;