日期:2014-05-17  浏览次数:20633 次

关于跨月计算的问题
本来是按月求和的 
(select  sum(Actual_D)from a where  Month(Dtime) = month(@DTime) and year(Dtime) = year(@DTime) and delete_flag=0) as Actual_sum_m,

现在要求 从每个月的26号开始到下个月的25号为一个月的,这个该怎么写 求大虾们指教啊

------解决方案--------------------
group by 后面写 
case when day(Dtime)>25 then month(Dtime)+1 else month(Dtime) end
------解决方案--------------------
用month,year函数不觉得慢吗,大概数据量不多吧
先把两个日期计算出来,然后再比较啊
@d1 = '2013-5-25'
@d2 = '2013-6-26'
(select  sum(Actual_D)from a where Dtime>=@d1 and Dtime<@d2 and delete_flag=0) as Actual_sum_m,
------解决方案--------------------
declare @DTime datetime
set @DTime='2013-5-25'

select sum(Actual_D)
from a 
where delete_flag=0 and
Dtime between 
(case when day(@DTime)>=26 then CONVERT(varchar(8),@DTime,23)+'26' 
  else CONVERT(varchar(8),dateadd(mm,-1,@DTime),23)+'26' end)
and 
(case when day(@DTime)>=26 then CONVERT(varchar(8),dateadd(mm,1,@DTime),23)+'25' 
      else CONVERT(varchar(8),@DTime,23)+'25' end)