我的一个表中有
仓库编号,仓库名称,产品编号,产品数量
001  仓库1   1001   10
001  仓库1   1020   150
001  仓库1   1100    200
002   仓库2  1111    30
003   仓库3  1100    30
003    仓库3  1103   60
有十多个仓库,每个仓库的库存产品的数量,现在是希望查询的结果显示为:
仓库1编号,仓库1名称,产品1的数量、产品2的数量…..
仓库编号,仓库名称,1000,1001  1003  1100  1111   1020
001      仓库1     0     0     0    200   0      150
  002      仓库2     0     0     0     0    30      0

解决方案 »

  1.   

    declare @sql varchar(8000)
    set @sql = 'select 仓库编号,仓库名称'
    select @sql = @sql + ',sum(case 产品编号 when '''+产品编号+''' then 产品数量 end) ['+产品编号+']'
     from (select distinct 产品编号 from 表) as a
    select @sql = @sql+' from 表 group by 仓库编号,仓库名称'
    exec(@sql)
      

  2.   

    select 仓库编号, 仓库名称,
        [1000]=sum(case 产品编号 when '1000' then 产品数量 end),
        [1001]=sum(case 产品编号 when '1000' then 产品数量 end),
        [1003]=sum(case 产品编号 when '1000' then 产品数量 end),
        [1100]=sum(case 产品编号 when '1000' then 产品数量 end),
        [1111]=sum(case 产品编号 when '1000' then 产品数量 end),
        [1020]=sum(case 产品编号 when '1000' then 产品数量 end)
    from tb
    group by 仓库编号, 仓库名称
      

  3.   

    create table tb(仓库编号 varchar(10),仓库名称 varchar(10),产品编号 varchar(10),产品数量 varchar(10))
    insert into tb select '001','仓库1',1001   ,10
    union all select '001'  ,'仓库1'   ,1020   ,150
    union all select '001'  ,'仓库1'   ,1100    ,200
    union all select '002'   ,'仓库2'  ,1111    ,30
    union all select '003'   ,'仓库3'  ,1100    ,30
    union all select '003'   ,'仓库3'  ,1103   ,60declare @sql varchar(8000)
    set @sql='select 仓库编号,仓库名称'
    select @sql=@sql+',['+cast(产品编号 as varchar)+']=sum(case 产品编号 when '''+cast(产品编号 as varchar)+''' then 产品数量 else 0 end)' from tb group by 产品编号
    exec(@sql+' from tb group by 仓库编号,仓库名称')drop table tb
      

  4.   

    -- SQL 2005SELECT *
    FROM(
    SELECT 仓库编号, 仓库名称, 产品编号, 产品数量 FROM tb
    )DATA
    PIVOT(
    SUM(产品数量)
    FOR 产品编号 IN([1000], [1001], [1003], [1100], [1111], [1020])
    )P
      

  5.   

    declare @sql varchar(8000)
    set @sql = 'select 仓库编号,仓库名称'
    select @sql = @sql + ',sum(case 产品编号 when '''+产品编号+''' then 产品数量 end) ['+产品编号+']'
     from (select distinct 产品编号 from 表) as a
    select @sql = @sql+' from 表 group by 仓库编号,仓库名称'
    exec(@sql)
      

  6.   

    create table #(仓库编号 nvarchar(100),仓库名称 nvarchar(100),产品编号 nvarchar(100),产品数量 int)
    insert into #
    select '001','仓库1','1001',10
    union all select '001','仓库1','1020',150
    union all select '001','仓库1','1100',200
    union all select '002','仓库2','1111',30
    union all select '003','仓库3','1100',30
    union all select '003','仓库3','1103',60declare @sql varchar(8000)
    select @sql=''
    select @sql=@sql+',isnull((select 产品数量 from # where 仓库编号=a.仓库编号 and 仓库名称=a.仓库名称 and 产品编号='''+产品编号+'''),0) as '''+产品编号+'''' from # group by 产品编号
    select @sql='select 仓库编号,仓库名称'+@sql+'  from # a group by 仓库编号,仓库名称'
    exec(@sql)
    print @sql
    drop table #--(所影响的行数为 6 行)
    --(所影响的行数为 6 行)
    --结果  
    --仓库编号 仓库名称 1001  1020 1100 1111 1100 1103--001 仓库1 10 150 200 0 0
    --002 仓库2 0 0 0 0 30
    --003 仓库3 0 0 30 60 0