q09.sql 610 B

1234567891011121314151617181920212223242526272829303132333435
  1. --!syntax_pg
  2. --TPC-H Q9
  3. select
  4. nation,
  5. o_year,
  6. sum(amount) as sum_profit
  7. from (
  8. select
  9. n_name as nation,
  10. extract(year from o_orderdate) as o_year,
  11. l_extendedprice * (1::numeric - l_discount) - ps_supplycost * l_quantity as amount
  12. from
  13. plato."part",
  14. plato."supplier",
  15. plato."lineitem",
  16. plato."partsupp",
  17. plato."orders",
  18. plato."nation"
  19. where
  20. s_suppkey = l_suppkey
  21. and ps_suppkey = l_suppkey
  22. and ps_partkey = l_partkey
  23. and p_partkey = l_partkey
  24. and o_orderkey = l_orderkey
  25. and s_nationkey = n_nationkey
  26. and p_name like '%green%'
  27. ) as profit
  28. group by
  29. nation,
  30. o_year
  31. order by
  32. nation,
  33. o_year desc;