Reputation: 6676
I have an order table, and it has a datetime column called order_date. I want to count the number of orders for each month in 2009. How would I do that?
Upvotes: 3
Views: 3317
Reputation: 1352
What about using some neat DATETIME trick?
select
DATEADD(MONTH, DATEDIFF(MONTH, 0, order_date), 0) AS orderMonth, count(*) as orderCount
from
[order]
where
order_date >= '2009-01-01'
group by
DATEADD(MONTH, DATEDIFF(MONTH, 0, order_date), 0)
order by
orderMonth
Upvotes: 0
Reputation: 51925
SELECT MONTH(order_date) AS ordermonth,
COUNT(*) AS ordercount
FROM order
WHERE YEAR(order_date) = 2009
GROUP BY ordermonth;
Upvotes: 1