q4a :: Day -> Q [(Text, Integer)]
q4a startDate =
  sortWith fst
  $ map (\(view -> (k, g)) -> pair k (length g))
  $ groupWithKey id
    [ o_orderpriorityQ o
    | o <- orders
    , o_orderdateQ o >= toQ startDate
    , o_orderdateQ o < toQ endDate
    , not $ null [ toQ ()
                 | l <- lineitems
                 , l_orderkeyQ l == o_orderkeyQ o
                 , l_commitdateQ l < l_receiptdateQ l
                 ]
    ]

  where
    endDate = C.addGregorianMonthsRollOver 3 startDate

--------------------------------------------------------------------------------

-- | TPC-H Query Q4 (literal transcription with 'any')
q4a :: Day -> Q [(Text, Integer)]
q4a startDate =
  sortWith fst
  $ map (\(view -> (k, g)) -> pair k (length g))
  $ groupWithKey id
    [ o_orderpriorityQ o
    | o <- orders
    , o_orderdateQ o >= toQ startDate
    , o_orderdateQ o < toQ endDate
    , not $ null [ toQ ()
                 | l <- lineitems
                 , l_orderkeyQ l == o_orderkeyQ o
                 , l_commitdateQ l < l_receiptdateQ l
                 ]
    ]

  where
    endDate = C.addGregorianMonthsRollOver 3 startDate

--------------------------------------------------------------------------------

-- | TPC-H Query Q4 (literal transcription with 'any')
q4a startDate =
  sortWith fst
  $ map (\(view -> (k, g)) -> pair k (length g))
  $ groupWithKey id
    [ o_orderpriorityQ o
    | o <- orders
    , o_orderdateQ o >= toQ startDate
    , o_orderdateQ o < toQ endDate
    , not $ null [ toQ ()
                 | l <- lineitems
                 , l_orderkeyQ l == o_orderkeyQ o
                 , l_commitdateQ l < l_receiptdateQ l
                 ]
    ]

  where
    endDate = C.addGregorianMonthsRollOver 3 startDate

--------------------------------------------------------------------------------

-- | TPC-H Query Q4 (literal transcription with 'any')