select
本月=sum(case when datediff(month,fdate,getdate())=0 then fAmount else 0 end),
本年=sum(case when datediff(year,fdate,getdate())=0 then fAmount else 0 end),
上年同期=sum(case when datediff(month,fdate,getdate())=1 then fAmount else 0 end)
from tblA

解决方案 »

  1.   

    --如果本月是指 2004年11月
    select 本月=(select sum(fAmount) from tblA where convert(char(7),fdate,120)='2004-11')
    ,本年=(select sum(fAmount) from tblA where year(fdate)=2004)
    ,上年同期=(select sum(fAmount) from tblA where convert(char(7),fdate,120)='2003-11')
      

  2.   

    写错了点,上年同期应该相差12个月:
    select
    本月=sum(case when datediff(month,fdate,getdate())=0 then fAmount else 0 end),
    本年=sum(case when datediff(year,fdate,getdate())=0 then fAmount else 0 end),
    上年同期=sum(case when datediff(month,fdate,getdate())=12 then fAmount else 0 end)
    from tblA
      

  3.   

    --用个参数来控制就是这样
    declare @年月 char(7)
    set @年月='2004-11'select 本月=(select sum(fAmount) from tblA where datediff(month,fdate,@年月+'-1')=0)
    ,本年=(select sum(fAmount) from tblA where datediff(year,fdate,@年月+'-1')=0)
    ,上年同期=(select sum(fAmount) from tblA where datediff(month,fdate,@年月+'-1')=12)
      

  4.   

    select '本月'=sum(case when month(fdate)=month(getdate()) then fAmount
                 else 0
                end ),
            '本年'=sum(case when year(fdate)=year(getdate()) then fAmount
                 else 0
                end ),
             
            ' 上年同期'=sum(case when year(fdate)=2003 and month(fdate)=11  then fAmount
                 else 0
                end )
    from tblA
      

  5.   

    convert(char(7),fdate,120)的参数能解释一下吗?