有个SQL表,共三列:  类型      日期       数量
                     计算机    2010-5-5    5个
                     计算机    2010-5-18   5个 
                     计算机    2010-6-5    5个
                     笔记本    2010-9-5    9个
                     鼠标      2010-1-6    10个
                     键盘      2010-5-17   12个
现在我需要将它变成: 类型      01  02  03  04  05  06  07  08  09  10  11  12(这些是月份,可能行列对不齐请见谅)
                     计算机                    10个 5个
                     笔记本                                    9个
                     鼠标      10个
                     键盘                      12个
问题:首先,需要将日期中的月份查出来用于第二个表的横轴,
      其次,显示某一类型物品在某月下的数量,有时需要合计请问这该如何实现,谢谢!
 

解决方案 »

  1.   

    select 类型,
    sum(case when month(日期)=1 then 数量 else 0 end) as month01,
    sum(case when month(日期)=2 then 数量 else 0 end) as month02,
    sum(case when month(日期)=3 then 数量 else 0 end) as month03,
    sum(case when month(日期)=4 then 数量 else 0 end) as month04,
    sum(case when month(日期)=5 then 数量 else 0 end) as month05,
    sum(case when month(日期)=6 then 数量 else 0 end) as month06,
    sum(case when month(日期)=7 then 数量 else 0 end) as month07,
    sum(case when month(日期)=8 then 数量 else 0 end) as month08,
    sum(case when month(日期)=9 then 数量 else 0 end) as month09,
    sum(case when month(日期)=10 then 数量 else 0 end) as month10,
    sum(case when month(日期)=11 then 数量 else 0 end) as month11,
    sum(case when month(日期)=12 then 数量 else 0 end) as month12
    from tablename 
    group by 类型
      

  2.   


    哈哈哈哈,在SQL呆了2个星期,速度就练出来了
      

  3.   

    select 类型,
    sum(case when month(日期)=1 then 数量 else 0 end) as 1,
    sum(case when month(日期)=2 then 数量 else 0 end) as 2,
    sum(case when month(日期)=3 then 数量 else 0 end) as 3,
    sum(case when month(日期)=4 then 数量 else 0 end) as 4,
    sum(case when month(日期)=5 then 数量 else 0 end) as 5,
    sum(case when month(日期)=6 then 数量 else 0 end) as 6,
    sum(case when month(日期)=7 then 数量 else 0 end) as 7,
    sum(case when month(日期)=8 then 数量 else 0 end) as 8,
    sum(case when month(日期)=9 then 数量 else 0 end) as 9,
    sum(case when month(日期)=10 then 数量 else 0 end) as 10,
    sum(case when month(日期)=11 then 数量 else 0 end) as 11,
    sum(case when month(日期)=12 then 数量 else 0 end) as 12
    from tablename 
    group by 类型
      

  4.   

      --> 测试数据:[TB]
    if object_id('[TB]') is not null drop table [TB]
    create table [TB]([类型] varchar(6),[日期] datetime,[数量] varchar(4))
    insert [TB]
    select '计算机','2010-5-5','5个' union all
    select '计算机','2010-5-18','5个' union all
    select '计算机','2010-6-5','5个' union all
    select '笔记本','2010-9-5','9个' union all
    select '鼠标','2010-1-6','10个' union all
    select '键盘','2010-5-17','12个'select * from [TB]
    select [类型] as [类型] ,
      max(case DATEPART(m,[日期]) when 1 then [数量] else '0' end) AS [1],
      max(case DATEPART(m,[日期]) when 2 then [数量] else '0' end) AS [2],
      max(case DATEPART(m,[日期]) when 3 then [数量] else '0' end) AS [3],
      max(case DATEPART(m,[日期]) when 4 then [数量] else '0' end) AS [4],
      max(case DATEPART(m,[日期]) when 5 then [数量] else '0' end) AS [5],
      max(case DATEPART(m,[日期]) when 6 then [数量] else '0' end) AS [6],
      max(case DATEPART(m,[日期]) when 7 then [数量] else '0' end) AS [7],                                                                                                                                  
      max(case DATEPART(m,[日期]) when 8 then [数量] else '0' end) AS [8],
      max(case DATEPART(m,[日期]) when 9 then [数量] else '0' end) AS [9],
      max(case DATEPART(m,[日期]) when 10 then [数量] else '0' end) AS [10],                                                                   
      max(case DATEPART(m,[日期]) when 11 then [数量] else '0' end) AS [11],
      max(case DATEPART(m,[日期]) when 12 then [数量] else '0' end) AS [12]
    from tb
    group by [类型]/*
    类型     1    2    3    4    5    6    7    8    9    10   11   12
    ------ ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
    笔记本    0    0    0    0    0    0    0    0    9个   0    0    0
    计算机    0    0    0    0    5个   5个   0    0    0    0    0    0
    键盘     0    0    0    0    12个  0    0    0    0    0    0    0
    鼠标     10个  0    0    0    0    0    0    0    0    0    0    0(4 行受影响)
    */
      

  5.   


    楼主,我的方法不能有单位的,有单位的话
    select 类型,
    sum(case when month(日期)=1 then convert(int,left(数量,len(数量)-1)) else 0 end) as month01,
    sum(case when month(日期)=2 then convert(int,left(数量,len(数量)-1)) else 0 end) as month02,
    sum(case when month(日期)=3 then convert(int,left(数量,len(数量)-1)) else 0 end) as month03,
    sum(case when month(日期)=4 then convert(int,left(数量,len(数量)-1)) else 0 end) as month04,
    sum(case when month(日期)=5 then convert(int,left(数量,len(数量)-1)) else 0 end) as month05,
    sum(case when month(日期)=6 then convert(int,left(数量,len(数量)-1)) else 0 end) as month06,
    sum(case when month(日期)=7 then convert(int,left(数量,len(数量)-1)) else 0 end) as month07,
    sum(case when month(日期)=8 then convert(int,left(数量,len(数量)-1)) else 0 end) as month08,
    sum(case when month(日期)=9 then convert(int,left(数量,len(数量)-1)) else 0 end) as month09,
    sum(case when month(日期)=10 then convert(int,left(数量,len(数量)-1)) else 0 end) as month10,
    sum(case when month(日期)=11 then convert(int,left(数量,len(数量)-1)) else 0 end) as month11,
    sum(case when month(日期)=12 then convert(int,left(数量,len(数量)-1)) else 0 end) as month12
    from tablename 
    group by 类型
      

  6.   

    to 路飞:我用你这个方法运行时,会报MONTH标识符无效
      

  7.   

          这个month会认为是无效标示符啊!
      

  8.   

    create table test2
    (name nvarchar(20),
    date datetime,
    num int)insert into test2
    select   '计算机','2010-5-5',5 union all
    select '计算机','2010-5-18',5  union all
    select '计算机','2010-6-5',5union all
    select '笔记本','2010-9-5',9union all
    select '鼠标','2010-1-6',10union all
    select '键盘','2010-5-17',12 select * from (select name,month(date)as m,num  from test2 )a  pivot (sum(num) for m in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) as pvtname                 1           2           3           4           5           6           7           8           9           10          11          12
    -------------------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
    笔记本                  NULL        NULL        NULL        NULL        NULL        NULL        NULL        NULL        9           NULL        NULL        NULL
    计算机                  NULL        NULL        NULL        NULL        10          5           NULL        NULL        NULL        NULL        NULL        NULL
    键盘                   NULL        NULL        NULL        NULL        12          NULL        NULL        NULL        NULL        NULL        NULL        NULL
    鼠标                   10          NULL        NULL        NULL        NULL        NULL        NULL        NULL        NULL        NULL        NULL        NULL(4 行受影响)
      

  9.   

    这个计算的时候,肯定不能有单位,除非运算的时候,数据再处理过!
    用month没什么问题啊!LZ报什么错误?
      

  10.   

    报的是MONTH为无效标识符,现在已经解决了!
    select equiptype,
    sum(case when to_char(appldate,'mm')=1 then applqty else '0' end) as month01,
    sum(case when to_char(appldate,'mm')=2 then applqty else '0' end) as month02,
    sum(case when to_char(appldate,'mm')=3 then applqty else '0' end) as month03,
    sum(case when to_char(appldate,'mm')=4 then applqty else '0' end) as month04,
    sum(case when to_char(appldate,'mm')=5 then applqty else '0' end) as month05,
    sum(case when to_char(appldate,'mm')=6 then applqty else '0' end) as month06,
    sum(case when to_char(appldate,'mm')=7 then applqty else '0' end) as month07,
    sum(case when to_char(appldate,'mm')=8 then applqty else '0' end) as month08,
    sum(case when to_char(appldate,'mm')=9 then applqty else '0' end) as month09,
    sum(case when to_char(appldate,'mm')=10 then applqty else '0' end) as month10,
    sum(case when to_char(appldate,'mm')=11 then applqty else '0' end) as month11,
    sum(case when to_char(appldate,'mm')=12 then applqty else '0' end) as month12
    from I_EQUIPMENT_BUDGET group by equiptype