q22.sql 585 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. --!syntax_pg
  2. --TPC-H Q22
  3. select
  4. cntrycode,
  5. count(*) as numcust,
  6. sum(c_acctbal) as totacctbal
  7. from (
  8. select
  9. substring(c_phone from 1 for 2) as cntrycode,
  10. c_acctbal
  11. from
  12. plato."customer"
  13. where
  14. substring(c_phone from 1 for 2) in
  15. ('13','31','23','29','30','18','17')
  16. and c_acctbal > (
  17. select
  18. avg(c_acctbal)
  19. from
  20. plato."customer"
  21. where
  22. c_acctbal > 0.00::numeric
  23. and substring (c_phone from 1 for 2) in
  24. ('13','31','23','29','30','18','17')
  25. )
  26. and not exists (
  27. select
  28. *
  29. from
  30. plato."orders"
  31. where
  32. o_custkey = c_custkey
  33. )
  34. ) as custsale
  35. group by
  36. cntrycode
  37. order by
  38. cntrycode;