tpch_query22.sql 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. drop view q22_customer_tmp_cached;
  2. drop view q22_customer_tmp1_cached;
  3. drop view q22_orders_tmp_cached;
  4. create view if not exists q22_customer_tmp_cached as
  5. select
  6. c_acctbal,
  7. c_custkey,
  8. substr(c_phone, 1, 2) as cntrycode
  9. from
  10. customer
  11. where
  12. substr(c_phone, 1, 2) = '13' or
  13. substr(c_phone, 1, 2) = '31' or
  14. substr(c_phone, 1, 2) = '23' or
  15. substr(c_phone, 1, 2) = '29' or
  16. substr(c_phone, 1, 2) = '30' or
  17. substr(c_phone, 1, 2) = '18' or
  18. substr(c_phone, 1, 2) = '17';
  19. create view if not exists q22_customer_tmp1_cached as
  20. select
  21. avg(c_acctbal) as avg_acctbal
  22. from
  23. q22_customer_tmp_cached
  24. where
  25. c_acctbal > 0.00;
  26. create view if not exists q22_orders_tmp_cached as
  27. select
  28. o_custkey
  29. from
  30. orders
  31. group by
  32. o_custkey;
  33. select
  34. cntrycode,
  35. count(1) as numcust,
  36. sum(c_acctbal) as totacctbal
  37. from (
  38. select
  39. cntrycode,
  40. c_acctbal,
  41. avg_acctbal
  42. from
  43. q22_customer_tmp1_cached ct1 join (
  44. select
  45. cntrycode,
  46. c_acctbal
  47. from
  48. q22_orders_tmp_cached ot
  49. right outer join q22_customer_tmp_cached ct
  50. on ct.c_custkey = ot.o_custkey
  51. where
  52. o_custkey is null
  53. ) ct2
  54. ) a
  55. where
  56. c_acctbal > avg_acctbal
  57. group by
  58. cntrycode
  59. order by
  60. cntrycode;