-- $Id: tpch04.sql 2657 2007-06-12 16:08:15Z rdempsey $
-- TPC-H/TPC-R Order Priority Checking Query (Q4)
-- Functional Query Definition
-- Approved February 1998

define 1 = 1993-07-01

select
	o_orderpriority,
	count(*) as order_count
from
	orders
where
	o_orderdate >= date '&1'
	and o_orderdate < date '&1' + interval '3' month
	and exists (
		select
			*
		from
			lineitem
		where
			l_orderkey = o_orderkey
			and l_commitdate < l_receiptdate
	)
group by
	o_orderpriority
order by
	o_orderpriority;

