开发者

counting number of entries in a month

开发者 https://www.devze.com 2022-12-17 20:57 出处:网络
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?select month(order_date) as orderMo

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?


select month(order_date) as orderMonth, count(*) as orderCount
from order
where year(order_date) = 2009
group by month(order_date)
order by month(order_date)

For reference, see month and year commands in Transact-SQL.


SELECT MONTH(order_date) AS ordermonth, 
   COUNT(*) AS ordercount 
FROM order 
WHERE YEAR(order_date) = 2009 
GROUP BY ordermonth;


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
0

精彩评论

暂无评论...
验证码 换一张
取 消