解决方案 »

  1.   

    假设你的数据都是2014年的,下面的sql针对你题中的数据写的。select case when yf=9 then '九月收入' 
                when yf=10 then '十月收入'
                else '十一月收入'
                end as yfsr,
    SUM(xyf) as xyf,
    SUM(cwf) as cwf,
    SUM(bgyf) as bgyf 
    from (
    select 
    *,
    case when cast(right([date],2) as int)<=20 then cast(right([date],2) as int) else cast(right([date],2)  as int)+1  end as yf
    from tablename 
    ) as a where yf in (9,10,11)
      

  2.   

    忘记group by 了,小调一下select case when yf=9 then '九月收入' 
                when yf=10 then '十月收入'
                else '十一月收入'
                end as yfsr,
    SUM(xyf) as xyf,
    SUM(cwf) as cwf,
    SUM(bgyf) as bgyf 
    from (
    select 
    *,
    case when cast(right([date],2) as int)<=20 then cast(right([date],2) as int) else cast(right([date],2)  as int)+1  end as yf
    from tablename 
    ) as a where yf in (9,10,11) group by yf
      

  3.   

    [code=sql]
    with cte as 
    (select case when DATE>=20140821 and DATE<20140921 then '九月收入'
                when DATE>=20140921 and DATE<20141021 then '十月收入'
                when DATE>=20141021 and DATE<20141121 then '十月收入'
                end as date
                ,xyf,cwf,bgyf from tablename )
                
                select date,SUM(xyf) as xyf,SUM(cwf)as cwf, SUM(bgyf)as bgyf
                 from cte 
                group by date /code]
      

  4.   

    现在改为通用的,试一下下面的sqlselect ny,SUM(xyf) as xyf,SUM(cwf) as cwf,SUM(bgyf) as bgyf 
    from (
    select *,
    case when cast(right([date],2) as int)<=20 then cast(substring([date],3,4)
         when cast(right([date],2) as int)>20 and cast(substring([date],5,2) as int)!=12 then cast(substring([date],3,4) as int)+1
         else cast(cast(substring([date],3,2)+1 as varchar(2))+'01' as int) end as ny
    from tablename ) as a group by ny
      

  5.   


    with cte as 
    (select case when right(DATE,2)>=21  then DATEADD(MONTH,1,date)
    else date end as  date
                ,xyf,cwf,bgyf from tablename )
                
                select date,SUM(xyf) as xyf,SUM(cwf)as cwf, SUM(bgyf)as bgyf
                 from cte 
                group by LEFT(date,4)试试这个。 判断DATE列后2位。如果是大于20,把月份加1,(用dateadd加1 就不会出现13月份的问题了。)
    然后在对LEFT(date,4)进行分组。