q8.sql 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. -- TPC-H/TPC-R National Market Share Query (Q8)
  2. -- TPC TPC-H Parameter Substitution (Version 2.17.2 build 0)
  3. -- using 1680793381 as a seed to the RNG
  4. $join1 = (
  5. select
  6. l.l_extendedprice * (1 - l.l_discount) as volume,
  7. l.l_suppkey as l_suppkey,
  8. l.l_orderkey as l_orderkey
  9. from
  10. plato.part as p
  11. join
  12. plato.lineitem as l
  13. on
  14. p.p_partkey = l.l_partkey
  15. where
  16. p.p_type = 'ECONOMY PLATED COPPER'
  17. );
  18. $join2 = (
  19. select
  20. j.volume as volume,
  21. j.l_orderkey as l_orderkey,
  22. s.s_nationkey as s_nationkey
  23. from
  24. $join1 as j
  25. join
  26. plato.supplier as s
  27. on
  28. s.s_suppkey = j.l_suppkey
  29. );
  30. $join3 = (
  31. select
  32. j.volume as volume,
  33. j.l_orderkey as l_orderkey,
  34. n.n_name as nation
  35. from
  36. $join2 as j
  37. join
  38. plato.nation as n
  39. on
  40. n.n_nationkey = j.s_nationkey
  41. );
  42. $join4 = (
  43. select
  44. j.volume as volume,
  45. j.nation as nation,
  46. DateTime::GetYear(cast(o.o_orderdate as Timestamp)) as o_year,
  47. o.o_custkey as o_custkey
  48. from
  49. $join3 as j
  50. join
  51. plato.orders as o
  52. on
  53. o.o_orderkey = j.l_orderkey
  54. where cast(cast(o_orderdate as Timestamp) as Date) between Date('1995-01-01') and Date('1996-12-31')
  55. );
  56. $join5 = (
  57. select
  58. j.volume as volume,
  59. j.nation as nation,
  60. j.o_year as o_year,
  61. c.c_nationkey as c_nationkey
  62. from
  63. $join4 as j
  64. join
  65. plato.customer as c
  66. on
  67. c.c_custkey = j.o_custkey
  68. );
  69. $join6 = (
  70. select
  71. j.volume as volume,
  72. j.nation as nation,
  73. j.o_year as o_year,
  74. n.n_regionkey as n_regionkey
  75. from
  76. $join5 as j
  77. join
  78. plato.nation as n
  79. on
  80. n.n_nationkey = j.c_nationkey
  81. );
  82. $join7 = (
  83. select
  84. j.volume as volume,
  85. j.nation as nation,
  86. j.o_year as o_year
  87. from
  88. $join6 as j
  89. join
  90. plato.region as r
  91. on
  92. r.r_regionkey = j.n_regionkey
  93. where
  94. r.r_name = 'AFRICA'
  95. );
  96. select
  97. o_year,
  98. sum(case
  99. when nation = 'MOZAMBIQUE' then volume
  100. else 0
  101. end) / sum(volume) as mkt_share
  102. from
  103. $join7 as all_nations
  104. group by
  105. o_year
  106. order by
  107. o_year;