计算一个月内的条目数

时间:2010-01-25 15:54:05

标签: sql tsql

我有一个订单表,它有一个名为order_date的日期时间列。我想计算2009年每个月的订单数量。我该怎么做?

3 个答案:

答案 0 :(得分:6)

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)

供参考,请参阅Transact-SQL中的monthyear命令。

答案 1 :(得分:1)

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

答案 2 :(得分:0)

使用一些整洁的DATETIME技巧怎么样?

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