MiniDr
MiniDr

Reputation: 301

A Way To Count Result Based on Date

SELECT
    a.OrderSuffix AS 'OrderSuffix',
    COUNT(1) AS 'CountNew'
FROM
    dbo.Orders AS a,
    dbo.OrderStatus AS b
WHERE
    b.Status = 'Finished' AND
    a.OrderSuffix IN ('ABC', 'DEF', 'HIJ')
GROUP BY
    a.OrderSuffix

For this query above, I can get all total rows count for each order suffix. Is there a way to include a date as well?

E.g. I want to count all rows of 'ABC' whose dateField is greater than specifiedDate.

Upvotes: 0

Views: 40

Answers (1)

Xedni
Xedni

Reputation: 4695

If you want to filter rows before you aggregate, you can just put it in your WHERE clause.

If you want the comparison to be based on the aggregate (e.g. groups with, say, a max dateField greater than some value) you can use the HAVING clause.

Upvotes: 1

Related Questions