汇总上个月的数据

时间:2011-04-05 20:03:41

标签: sql

我无法总结上个月的数据。这是我到目前为止所拥有的

SELECT 
    Coalesce(Sum(LIN_PROD_RATE), 0) 
FROM 
    Stafford Totals 
WHERE 
    MONTH(t_stamp) = MONTH(DATEADD(month, -1, current_timestamp))

任何想法???

3 个答案:

答案 0 :(得分:2)

select coalesce(sum(LIN_PROD_RATE), 0)
from Stafford
where
  t_stamp >= dateadd(month, datediff(month, 0, current_timestamp)-1, 0) and
  t_stamp < dateadd(month, datediff(month, 0, current_timestamp), 0)

答案 1 :(得分:1)

Declare @last_month as datetime;
set @last_month = DATEADD(month, -1, getdate());

SELECT 
    Coalesce(Sum(LIN_PROD_RATE), 0) 
FROM 
    Stafford Totals 
WHERE 
    MONTH(t_stamp) = MONTH(@last_month)
    AND YEAR(t_stamp) = YEAR(@last_month)

答案 2 :(得分:0)

如果是MySQL:

select coalesce(sum(lin_prod_rate), 0) 
from stafford totals 
where date_format(t_stamp, '%Y-%m') = date_format(date_add(curdate(), interval -1 month), '%Y-%m')