请高手帮我看一下,我想实现的是
条件是Inv date 如果>2006年的,比如说是 Inv Date =‘2006/6/20’
那么 sum(Ext Price)是从2005/6/1 到 2005/6/20,也就是取去年这个日子所在月份
如果 Inv date <2005,比如是 Inv Date =‘2005/6/20’那么就取2005/6/1 到 2005/6/20,也就是Inv Date所在的这一年的当月
我用的语句是下面的,不对啊,我不知道怎么改? 
SELECT Brand, Area,[Inv Date],   
sum ([Ext Price]) as MTD05 
FROM   dbo.[SL00-FMCG-Sales] 
where  case when  [Inv Date]<CONVERT(DATETIME, '2006-1-1 00:00:00', 102) then  
[Inv Date] between DATEADD(mm, DATEDIFF(mm,0,[Inv Date]), 0) and [Inv Date]
when [Inv Date]>CONVERT(DATETIME, '2005-12-31 00:00:00', 102) 
then [Inv Date] between  DATEADD(mm, DATEDIFF(mm,0,DATEAdd(yy,-1,[Inv Date])), 0) and DATEAdd(yy,-1,[Inv Date])
group by Brand, Area,[Inv Date]
order by Brand,[Inv Date]

解决方案 »

  1.   

    看不明白? 不过应该是 
    case when 语句应该这样用 select a,
           sum(case when b>0 then c else d end) as o,
           sum(case when b=0 then e else f end) as p,
           ....  
    from table
    group by a提示一下,分是可以给的,不要“竟然没法输入分”.
    还有,陈述问题是,应表达清楚,语句格式应该让人看得明白。乱!!