q21.sql 674 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. --!syntax_pg
  2. --TPC-H Q21
  3. select
  4. s_name,
  5. count(*) as numwait
  6. from
  7. plato."supplier",
  8. plato."lineitem" l1,
  9. plato."orders",
  10. plato."nation"
  11. where
  12. s_suppkey = l1.l_suppkey
  13. and o_orderkey = l1.l_orderkey
  14. and o_orderstatus = 'F'
  15. and l1.l_receiptdate > l1.l_commitdate
  16. and exists (
  17. select
  18. *
  19. from
  20. plato."lineitem" l2
  21. where
  22. l2.l_orderkey = l1.l_orderkey
  23. and l2.l_suppkey <> l1.l_suppkey
  24. )
  25. and not exists (
  26. select
  27. *
  28. from
  29. plato."lineitem" l3
  30. where
  31. l3.l_orderkey = l1.l_orderkey
  32. and l3.l_suppkey <> l1.l_suppkey
  33. and l3.l_receiptdate > l3.l_commitdate
  34. )
  35. and s_nationkey = n_nationkey
  36. and n_name = 'SAUDI ARABIA'
  37. group by
  38. s_name
  39. order by
  40. numwait desc,
  41. s_name
  42. limit 100;