表1 
dept    fromdate    enddate       amount
 a     2007-12-24    2007-12-28      11
 b     2007-11-12    2007-11-25      22
 v     2005-1-4      2005-2-4        33
统计结果显示如下:
dept  一月  二月  三月  四月.....十二月  amount
请问这个怎么做啊

解决方案 »

  1.   

    不在同一年怎么办 
     b           2007-11-12         2007-11-25             22 
      v           2005-1-4 跨月的 amount 怎么解决
      

  2.   

    问题表述不太清晰吧?fromdate 是指开始时间?enddate结束时间?统计月份是自然月,并且按照fromdate和enddate之间吗?
      

  3.   

    对不起上面写错了,显示结果应该是
    dept     一月        二月      三月      四月.....  十二月     total
      a     amount    amount   amount  amount....  amount    amountd的总和 也就是统计这个部门每个月的amount          
      

  4.   

    select dept,
    [一月] = sum(case when datepart(mm,fromdate) = 1 then amount else 0 end),
    .....from table1
    group by dept
      

  5.   

    按 fromdate   来统计  enddate不管
      

  6.   

     v           2005-1-4             2005-2-4                 33有一月 有二月 amount 分在第几月啊 
      

  7.   

    select dept,
    [一月] = sum(case when datepart(mm,fromdate) = 1 then amount else 0 end),
    .....,
    [ total ] = sum(amount)
    from table1
    group by dept
      

  8.   

    select dept,
    [一月] = sum(case when datepart(mm,fromdate) = 1 then amount else 0 end),
    [二月] = sum(case when datepart(mm,fromdate) = 2 then amount else 0 end),
    .....,
    [十二月] = sum(case when datepart(mm,fromdate) = 12 then amount else 0 end),
    [ total ] = sum(amount)
    from table1
    group by dept
      

  9.   

    create table # (dept varchar(10),fromdate datetime,enddate datetime,amount int)insert into # values('a','2007-12-24','2007-12-28',11)
    insert into # values('b','2007-11-12','2007-11-25',22)
    insert into # values('v','2005-1-4','2005-2-4',33)select dept,一月=sum(case when datepart(month,fromdate)=1 then amount else 0 end),
    二月=sum(case when datepart(month,fromdate)=2 then amount else 0 end),
    三月=sum(case when datepart(month,fromdate)=3 then amount else 0 end),
    四月=sum(case when datepart(month,fromdate)=4 then amount else 0 end),
    五月=sum(case when datepart(month,fromdate)=5 then amount else 0 end),
    六月=sum(case when datepart(month,fromdate)=6 then amount else 0 end),
    七月=sum(case when datepart(month,fromdate)=7 then amount else 0 end),
    八月=sum(case when datepart(month,fromdate)=8 then amount else 0 end),
    九月=sum(case when datepart(month,fromdate)=9 then amount else 0 end),
    十月=sum(case when datepart(month,fromdate)=10 then amount else 0 end),
    十一月=sum(case when datepart(month,fromdate)=11 then amount else 0 end),
    十二月=sum(case when datepart(month,fromdate)=12 then amount else 0 end),
    amount=sum(amount)
    from # group by deptdept       一月          二月          三月          四月          五月          六月          七月          八月          九月          十月          十一月         十二月         amount
    ---------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
    a          0           0           0           0           0           0           0           0           0           0           0           11          11
    b          0           0           0           0           0           0           0           0           0           0           22          0           22
    v          33          0           0           0           0           0           0           0           0           0           0           0           33(3 行受影响)
      

  10.   

    Syntax error converting the nvarchar value '2020.999' to a column of data type int.
    这个错误怎么处理啊
      

  11.   

    select sum(case  when datepart(mm,enddate )=1 then 1 else 0 end) as 一月,
           sum(case  when datepart(mm,enddate )=2 then 1 else 0 end)as 二月,
    sum(case  when datepart(mm,enddate )=3 then 1 else 0 end) as 三月,
    sum(case  when datepart(mm,enddate )=4 then 1 else 0 end) as 四月,
    sum(case  when datepart(mm,enddate )=5 then 1 else 0 end) as 五月,
    sum(case  when datepart(mm,enddate )=6 then 1 else 0 end) as 六月,
    sum(case  when datepart(mm,enddate )=7 then 1 else 0 end) as 七月,
    sum(case  when datepart(mm,enddate )=8 then 1 else 0 end) as 八月,
    sum(case  when datepart(mm,enddate )=9 then 1 else 0 end) as 九月,
    sum(case  when datepart(mm,enddate )=10 then 1 else 0 end) as 十月,
    sum(case  when datepart(mm,enddate )=11 then 1 else 0 end) as 十一月,
    sum(case  when datepart(mm,enddate )=12 then 1 else 0 end) as 十二月
    from table
    group by dept
      

  12.   

    Syntax   error   converting   the   nvarchar   value   '2020.999'   to   a   column   of   data   type   int
    你的 amountd 是字符型的吧
      

  13.   

    Syntax   error   converting   the   nvarchar   value   '2020.999'   to   a   column   of   data   type   int
    你的 amountd 是字符型的吧
      

  14.   

    好象amount 不是整数的话就报错了
      

  15.   

    select dept,一月=sum(case when datepart(month,fromdate)=1 then cast(amount,float) else 0 end),
    二月=sum(case when datepart(month,fromdate)=2 then cast(amount,float) else 0 end),
    三月=sum(case when datepart(month,fromdate)=3 then cast(amount,float) else 0 end),
    四月=sum(case when datepart(month,fromdate)=4 then cast(amount,float) else 0 end),
    五月=sum(case when datepart(month,fromdate)=5 then cast(amount,float) else 0 end),
    六月=sum(case when datepart(month,fromdate)=6 then cast(amount,float) else 0 end),
    七月=sum(case when datepart(month,fromdate)=7 then cast(amount,float) else 0 end),
    八月=sum(case when datepart(month,fromdate)=8 then cast(amount,float) else 0 end),
    九月=sum(case when datepart(month,fromdate)=9 then cast(amount,float) else 0 end),
    十月=sum(case when datepart(month,fromdate)=10 then cast(amount,float) else 0 end),
    十一月=sum(case when datepart(month,fromdate)=11 then cast(amount,float) else 0 end),
    十二月=sum(case when datepart(month,fromdate)=12 then cast(amount,float) else 0 end),
    amount=sum(cast(amount,float))
    from # group by dept
      

  16.   

    上面错了
    用这个试试
    select dept,一月=sum(case when datepart(month,fromdate)=1 then cast(amount as float) else 0 end),
    二月=sum(case when datepart(month,fromdate)=2 then cast(amount as float) else 0 end),
    三月=sum(case when datepart(month,fromdate)=3 then cast(amount as float) else 0 end),
    四月=sum(case when datepart(month,fromdate)=4 then cast(amount as float) else 0 end),
    五月=sum(case when datepart(month,fromdate)=5 then cast(amount as float) else 0 end),
    六月=sum(case when datepart(month,fromdate)=6 then cast(amount as float) else 0 end),
    七月=sum(case when datepart(month,fromdate)=7 then cast(amount as float) else 0 end),
    八月=sum(case when datepart(month,fromdate)=8 then cast(amount as float) else 0 end),
    九月=sum(case when datepart(month,fromdate)=9 then cast(amount as float) else 0 end),
    十月=sum(case when datepart(month,fromdate)=10 then cast(amount as float) else 0 end),
    十一月=sum(case when datepart(month,fromdate)=11 then cast(amount as float) else 0 end),
    十二月=sum(case when datepart(month,fromdate)=12 then cast(amount as float) else 0 end),
    amount=sum(cast(amount as float))
    from # group by dept
      

  17.   

    Incorrect syntax near 'cast', expected 'AS'.
    报这个错啊
      

  18.   

    select dept,一月=sum(case when datepart(month,fromdate)=1 then cast(amount as float) else 0 end),
            二月=sum(case when datepart(month,fromdate)=2 then cast(amount as float) else 0 end),
            三月=sum(case when datepart(month,fromdate)=3 then cast(amount as float) else 0 end),
            四月=sum(case when datepart(month,fromdate)=4 then cast(amount as float) else 0 end),
            五月=sum(case when datepart(month,fromdate)=5 then cast(amount as float) else 0 end),
            六月=sum(case when datepart(month,fromdate)=6 then cast(amount as float) else 0 end),
            七月=sum(case when datepart(month,fromdate)=7 then cast(amount as float) else 0 end),
            八月=sum(case when datepart(month,fromdate)=8 then cast(amount as float) else 0 end),
            九月=sum(case when datepart(month,fromdate)=9 then cast(amount as float) else 0 end),
            十月=sum(case when datepart(month,fromdate)=10 then cast(amount as float) else 0 end),
            十一月=sum(case when datepart(month,fromdate)=11 then cast(amount as float) else 0 end),
            十二月=sum(case when datepart(month,fromdate)=12 then cast(amount as float) else 0 end),
            amount=sum(cast(amount as float))
    from # group by dept
      

  19.   

    保存两位小数
    select dept,一月=cast(sum(case when datepart(month,fromdate)=1 then cast(amount as float) else 0 end) as decimal(38,2)),
    二月=cast(sum(case when datepart(month,fromdate)=2 then cast(amount as float) else 0 end) as decimal(38,2)),
    三月=cast(sum(case when datepart(month,fromdate)=3 then cast(amount as float) else 0 end) as decimal(38,2)),
    四月=cast(sum(case when datepart(month,fromdate)=4 then cast(amount as float) else 0 end) as decimal(38,2)),
    五月=cast(sum(case when datepart(month,fromdate)=5 then cast(amount as float) else 0 end) as decimal(38,2)),
    六月=cast(sum(case when datepart(month,fromdate)=6 then cast(amount as float) else 0 end) as decimal(38,2)),
    七月=cast(sum(case when datepart(month,fromdate)=7 then cast(amount as float) else 0 end) as decimal(38,2)),
    八月=cast(sum(case when datepart(month,fromdate)=8 then cast(amount as float) else 0 end) as decimal(38,2)),
    九月=cast(sum(case when datepart(month,fromdate)=9 then cast(amount as float) else 0 end) as decimal(38,2)),
    十月=cast(sum(case when datepart(month,fromdate)=10 then cast(amount as float) else 0 end) as decimal(38,2)),
    十一月=cast(sum(case when datepart(month,fromdate)=11 then cast(amount as float) else 0 end) as decimal(38,2)),
    十二月=cast(sum(case when datepart(month,fromdate)=12 then cast(amount as float) else 0 end) as decimal(38,2)),
    amount=cast(sum(cast(amount as float)) as decimal(38,2))
    from # group by dept
      

  20.   

    我想在最后加一行专门显示每个月的总和的怎么搞啊
    dept 一月 二月..... 
    a     11   22
    b     22   10
    total  33   32
    上面的效果
      

  21.   

    我想在最后加一行专门显示每个月的总和的怎么搞啊 
    dept   一月   二月.....   
    a           11       22 
    b           22       10 
    total     33       32 
    上面的效果
    select dept,一月=cast(sum(case when datepart(month,fromdate)=1 then cast(amount as float) else 0 end) as decimal(38,2)),
            二月=cast(sum(case when datepart(month,fromdate)=2 then cast(amount as float) else 0 end) as decimal(38,2)),
            三月=cast(sum(case when datepart(month,fromdate)=3 then cast(amount as float) else 0 end) as decimal(38,2)),
            四月=cast(sum(case when datepart(month,fromdate)=4 then cast(amount as float) else 0 end) as decimal(38,2)),
            五月=cast(sum(case when datepart(month,fromdate)=5 then cast(amount as float) else 0 end) as decimal(38,2)),
            六月=cast(sum(case when datepart(month,fromdate)=6 then cast(amount as float) else 0 end) as decimal(38,2)),
            七月=cast(sum(case when datepart(month,fromdate)=7 then cast(amount as float) else 0 end) as decimal(38,2)),
            八月=cast(sum(case when datepart(month,fromdate)=8 then cast(amount as float) else 0 end) as decimal(38,2)),
            九月=cast(sum(case when datepart(month,fromdate)=9 then cast(amount as float) else 0 end) as decimal(38,2)),
            十月=cast(sum(case when datepart(month,fromdate)=10 then cast(amount as float) else 0 end) as decimal(38,2)),
            十一月=cast(sum(case when datepart(month,fromdate)=11 then cast(amount as float) else 0 end) as decimal(38,2)),
            十二月=cast(sum(case when datepart(month,fromdate)=12 then cast(amount as float) else 0 end) as decimal(38,2)),
            amount=cast(sum(cast(amount as float)) as decimal(38,2))
    from # group by dept
    union all
    select 'total',一月=cast(sum(case when datepart(month,fromdate)=1 then cast(amount as float) else 0 end) as decimal(38,2)),
            二月=cast(sum(case when datepart(month,fromdate)=2 then cast(amount as float) else 0 end) as decimal(38,2)),
            三月=cast(sum(case when datepart(month,fromdate)=3 then cast(amount as float) else 0 end) as decimal(38,2)),
            四月=cast(sum(case when datepart(month,fromdate)=4 then cast(amount as float) else 0 end) as decimal(38,2)),
            五月=cast(sum(case when datepart(month,fromdate)=5 then cast(amount as float) else 0 end) as decimal(38,2)),
            六月=cast(sum(case when datepart(month,fromdate)=6 then cast(amount as float) else 0 end) as decimal(38,2)),
            七月=cast(sum(case when datepart(month,fromdate)=7 then cast(amount as float) else 0 end) as decimal(38,2)),
            八月=cast(sum(case when datepart(month,fromdate)=8 then cast(amount as float) else 0 end) as decimal(38,2)),
            九月=cast(sum(case when datepart(month,fromdate)=9 then cast(amount as float) else 0 end) as decimal(38,2)),
            十月=cast(sum(case when datepart(month,fromdate)=10 then cast(amount as float) else 0 end) as decimal(38,2)),
            十一月=cast(sum(case when datepart(month,fromdate)=11 then cast(amount as float) else 0 end) as decimal(38,2)),
            十二月=cast(sum(case when datepart(month,fromdate)=12 then cast(amount as float) else 0 end) as decimal(38,2)),
            amount=cast(sum(cast(amount as float)) as decimal(38,2))
    from # dept       一月                                      二月                                      三月                                      四月                                      五月                                      六月                                      七月                                      八月                                      九月                                      十月                                      十一月                                     十二月                                     amount
    ---------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- ---------------------------------------
    a          0.00                                    0.00                                    0.00                                    0.00                                    0.00                                    0.00                                    0.00                                    0.00                                    0.00                                    0.00                                    0.00                                    11.00                                   11.00
    b          0.00                                    0.00                                    0.00                                    0.00                                    0.00                                    0.00                                    0.00                                    0.00                                    0.00                                    0.00                                    22.00                                   0.00                                    22.00
    v          33.00                                   0.00                                    0.00                                    0.00                                    0.00                                    0.00                                    0.00                                    0.00                                    0.00                                    0.00                                    0.00                                    0.00                                    33.00
    total      33.00                                   0.00                                    0.00                                    0.00                                    0.00                                    0.00                                    0.00                                    0.00                                    0.00                                    0.00                                    22.00                                   11.00                                   66.00(4 行受影响)