类型  数量  姓名
a      1     张三
a      2     李四
b      2     李四
b      2     张三
c      3     张三
c      3     李四
d      4     李四
d      4     张三
e      5     张三
e      5     李四
结果
姓名  a     b     c      d     e
-------------------------------------
张三  1     2     3      4     5
李四  2     2     3      4     5
类型不固定,请问应该怎么写sql语句

解决方案 »

  1.   

    create table tb(类型 varchar(1),数量 int,姓名 varchar(10))
    insert into tb
    select 'a', 1, '张三' union all
    select 'a', 2, '李四' union all
    select 'b', 2, '李四' union all
    select 'b', 2, '张三' union all
    select 'c', 3, '张三' union all
    select 'c', 3, '李四' union all
    select 'd', 4, '李四' union all
    select 'd', 4, '张三' union all
    select 'e', 5, '张三' union all
    select 'e', 5, '李四' union all
    select 'f', 1, '张三' union all
    select 'f', 1, '张三' union all
    select 'g', 1, '张三' union all
    select 'g', 1, '张三'
    DECLARE @SQL VARCHAR(8000)
    SELECT @SQL = ISNULL(@SQL + '],[' , '') + 类型 FROM tb GROUP BY 类型
    SET @SQL = '[' + @SQL + ']'
    EXEC('
                    SELECT    *
                    FROM (
                        SELECT    *
                        FROM    tb
                         ) A PIVOT (MAX(数量) FOR 类型 IN (' + @SQL + ')) B'
    )
    drop table tb--------------------------------------------------------
    姓名         a           b           c           d           e           f           g
    ---------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
    李四         2           2           3           4           5           NULL        NULL
    张三         1           2           3           4           5           1           1
      

  2.   

    select
      name,
      max(case 类型 when 'a' then 数量  else 0 end) as 'a',
      max(case 类型 when 'b' then 数量  else 0 end) as 'b',
      max(case 类型 when 'c' then 数量  else 0 end) as 'c',
      max(case 类型 when 'd' then 数量  else 0 end) as 'd',
      max(case 类型 when 'e' then 数量  else 0 end) as 'e'
    from
      tb
    group by
      name
      

  3.   


    create table tb(类型 varchar(1),数量 int,姓名 varchar(10))insert into tb
    select 'a', 1, '张三' union all
    select 'a', 2, '李四' union all
    select 'b', 2, '李四' union all
    select 'b', 2, '张三' union all
    select 'c', 3, '张三' union all
    select 'c', 3, '李四' union all
    select 'd', 4, '李四' union all
    select 'd', 4, '张三' union all
    select 'e', 5, '张三' union all
    select 'e', 5, '李四'
    declare @sql varchar(2000)select @sql='select 姓名,'select @sql=@sql+'sum(case 类型 when '''+类型+''' then 数量 else 0 end) as '''+类型+''',' 
    from (select distinct 类型  from tb) yselect @sql=left(@sql,len(@sql)-1)select @sql=@sql+' from tb group by 姓名 order by 姓名 desc'exec(@sql)姓名         a           b           c           d           e
    ---------- ----------- ----------- ----------- ----------- -----------
    张三         1           2           3           4           5
    李四         2           2           3           4           5