有一張表兩個字段:_date,_num,
現在要產生如下的表格,請設計相關SQL語句。
年份 1月 2月 3月 …… 10月 11月 12月 
2010 數量 數量 數量 …… 數量 數量 數量
2011 數量 數量 數量 …… 數量 數量 數量
……

解决方案 »

  1.   


    declare @sql varchar(max)
    set @sql = 'select convert(varchar(4),_date,120)'
    select @sql = @sql + ',max(case convert(varchar(6),_date,112) when convert(varchar(4),_date,112) + ''' + ltrim(_date) + ''' then _num else 0 end)[' + ltrim(_date) + ']'
    from (select month(_date) from tb)t
    select @sql = @sql + ' from tb group by convert(varchar(4),_date,120)'
    exec(@sql)
      

  2.   

    select datepart(year,_date) [年份],
           sum( case when datepart(month,_date)=1 then _num else 0 end) [1月],
           sum( case when datepart(month,_date)=2 then _num else 0 end) [2月],
           --......
    from tb group by datepart(year,_date)
      

  3.   

    呵呵...sql函数还必须去练啊...
      

  4.   

    http://topic.csdn.net/u/20091013/15/9f058df7-4d29-47bf-a338-b63fcab2abc0.html
      

  5.   


    select nYear as 年份,(case id when 1 then '一月份' else '' end) as 一月份 ,
                 (case id when 2 then '二月份' else '' end) as 二月份 ,
                 (case id when 3 then '三月份' else '' end) as 三月份 ,
                 (case id when 4 then '四月份' else '' end) as 四月份 ,
                 (case id when 5 then '五月份' else '' end) as 五月份,
                 (case id when 6 then '六月份' else '' end) as 六月份 ,
                 (case id when 7 then '七月份' else '' end) as 七月份 ,
                 (case id when 8 then '八月份' else '' end) as 八月份 
                  from Table_Time
    //改改列名条件  应该可以
      

  6.   


    2F回答的不錯,可是沒看懂題目,有一些瑕疵
    其中convert(varchar(4),_date,120)可以修改為year(_date),max()應該使用sum(),convert(varchar(6),_date,112)可以修改為month(_date),對於select month(_date) from tb是不是少了group by month(_date)子句?最後完整code如下:
    declare @sql varchar(max)
    set @sql = 'select year(_date) [年份]'
    select @sql = @sql + ',sum(case month(_date) when ''' + ltrim(_date) + ''' then _num else 0 end) [' + ltrim(_date) + '月]'
    from (select month(_date) _date from dbo.Test group by month(_date)) t
    select @sql = @sql + ' from Test group by year(_date)'
    exec(@sql)
      

  7.   


    這個不錯,當時面試我就是這樣做的,但是沒有用datepart()函數,直接用year()和month()函數的
      

  8.   


    select year(_date) as 年份,
    Sum(case when month(_date)='01' then _num else 0 end ) as 一月,
    Sum(case when month(_date)='02' then _num else 0 end ) as 二月,
    Sum(case when month(_date)='03' then _num else 0 end ) as 三月,
    Sum(case when month(_date)='04' then _num else 0 end ) as 四月,
    Sum(case when month(_date)='05' then _num else 0 end ) as 五月,
    Sum(case when month(_date)='06' then _num else 0 end ) as 六月,
    Sum(case when month(_date)='07' then _num else 0 end ) as 七月,
    Sum(case when month(_date)='08' then _num else 0 end ) as 八月,
    Sum(case when month(_date)='09' then _num else 0 end ) as 九月,
    Sum(case when month(_date)='10' then _num else 0 end ) as 十月,
    Sum(case when month(_date)='11' then _num else 0 end ) as 十一月,
    Sum(case when month(_date)='12' then _num else 0 end ) as 十二月
     from #tryTable group by year(_date)