select 
sum (case CAST(month(Date) as varchar) when '1' then Qty else 0 end ) as a1,
sum (case CAST(month(Date) as varchar) when '2' then Qty else 0 end ) as a2,
sum (case CAST(month(Date) as varchar) when '3' then Qty else 0 end ) as a3,
sum (case CAST(month(Date) as varchar) when '4' then Qty else 0 end ) as a4,
sum (case CAST(month(Date) as varchar) when '5' then Qty else 0 end ) as a5,
sum (case CAST(month(Date) as varchar) when '6' then Qty else 0 end ) as a6,
sum (case CAST(month(Date) as varchar) when '7' then Qty else 0 end ) as a7,
sum (case CAST(month(Date) as varchar) when '8' then Qty else 0 end ) as a8,
sum (case CAST(month(Date) as varchar) when '9' then Qty else 0 end ) as a9,
sum (case CAST(month(Date) as varchar) when '10' then Qty else 0 end ) as a10,
sum (case CAST(month(Date) as varchar) when '11' then Qty else 0 end ) as a11,
sum (case CAST(month(Date) as varchar) when '12' then Qty else 0 end ) as a12
from Product

解决方案 »

  1.   

    select CAST(month(Date) as varchar) as a, sum(qty) as b from Product group by CAST(month(Date) as varchar)没有值的月份,我也不清楚怎么写...
      

  2.   

    select distinct a.qty,convert(varchar(10),datepart(yy,b.date))
    +'-'+convert(varchar(10),datepart(m,b.date)) from 
    (select sum(qty) qty ,datepart(m,date) date from tb1 group by datepart(m,date))a join
    tb1 b
    on datepart(m,b.date)=a.date 
    order by
    convert(varchar(10),datepart(yy,b.date))
    +'-'+convert(varchar(10),datepart(m,b.date))
      

  3.   

    那如果结果是这样,怎么写呢?
    2004-01  0
    2004-02  0
    2004-03  70
    2004-04  0
    2004-05  20
    2004-06  10
    ______________________________________________select Date,sum(qty) as SumQty from Product group by left(convert(varchar,Date,120),7)