q9.sql 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. -- TPC-H/TPC-R Product Type Profit Measure Query (Q9)
  2. -- Approved February 1998
  3. -- using 1680793381 as a seed to the RNG
  4. $p = (select p_partkey, p_name
  5. from
  6. plato.part
  7. where FIND(p_name, 'rose') IS NOT NULL);
  8. $j1 = (select ps_partkey, ps_suppkey, ps_supplycost
  9. from
  10. plato.partsupp as ps
  11. join $p as p
  12. on ps.ps_partkey = p.p_partkey);
  13. $j2 = (select l_suppkey, l_partkey, l_orderkey, l_extendedprice, l_discount, ps_supplycost, l_quantity
  14. from
  15. plato.lineitem as l
  16. join $j1 as j
  17. on l.l_suppkey = j.ps_suppkey AND l.l_partkey = j.ps_partkey);
  18. $j3 = (select l_orderkey, s_nationkey, l_extendedprice, l_discount, ps_supplycost, l_quantity
  19. from
  20. plato.supplier as s
  21. join $j2 as j
  22. on j.l_suppkey = s.s_suppkey);
  23. $j4 = (select o_orderdate, l_extendedprice, l_discount, ps_supplycost, l_quantity, s_nationkey
  24. from
  25. plato.orders as o
  26. join $j3 as j
  27. on o.o_orderkey = j.l_orderkey);
  28. $j5 = (select n_name, o_orderdate, l_extendedprice, l_discount, ps_supplycost, l_quantity
  29. from
  30. plato.nation as n
  31. join $j4 as j
  32. on j.s_nationkey = n.n_nationkey
  33. );
  34. $profit = (select
  35. n_name as nation,
  36. DateTime::GetYear(cast(o_orderdate as timestamp)) as o_year,
  37. l_extendedprice * (1 - l_discount) - ps_supplycost * l_quantity as amount
  38. from $j5);
  39. select
  40. nation,
  41. o_year,
  42. sum(amount) as sum_profit
  43. from $profit
  44. group by
  45. nation,
  46. o_year
  47. order by
  48. nation,
  49. o_year desc;