q22.sql 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. -- TPC-H/TPC-R Global Sales Opportunity Query (Q22)
  2. -- TPC TPC-H Parameter Substitution (Version 2.17.2 build 0)
  3. -- using 1680793381 as a seed to the RNG
  4. $customers = (
  5. select
  6. c_acctbal,
  7. c_custkey,
  8. Substring(c_phone, 0u, 2u) as cntrycode
  9. from
  10. plato.customer
  11. where (Substring(c_phone, 0u, 2u) = '31' or Substring(c_phone, 0u, 2u) = '29' or Substring(c_phone, 0u, 2u) = '30' or Substring(c_phone, 0u, 2u) = '26' or Substring(c_phone, 0u, 2u) = '28' or Substring(c_phone, 0u, 2u) = '25' or Substring(c_phone, 0u, 2u) = '15')
  12. );
  13. $avg = (
  14. select
  15. avg(c_acctbal) as a
  16. from
  17. $customers
  18. where
  19. c_acctbal > 0.00
  20. );
  21. $join1 = (
  22. select
  23. c.c_acctbal as c_acctbal,
  24. c.c_custkey as c_custkey,
  25. c.cntrycode as cntrycode
  26. from
  27. $customers as c
  28. cross join
  29. $avg as a
  30. where
  31. c.c_acctbal > a.a
  32. );
  33. $join2 = (
  34. select
  35. j.cntrycode as cntrycode,
  36. c_custkey,
  37. j.c_acctbal as c_acctbal
  38. from
  39. $join1 as j
  40. left only join
  41. plato.orders as o
  42. on
  43. o.o_custkey = j.c_custkey
  44. );
  45. select
  46. cntrycode,
  47. count(*) as numcust,
  48. sum(c_acctbal) as totacctbal
  49. from
  50. $join2 as custsale
  51. group by
  52. cntrycode
  53. order by
  54. cntrycode;