Select 台帐编号,煤炭品种,存储方式,sum(库存数量) as 库存数量
from 表名
group by 台帐编号,煤炭品种,存储方式

解决方案 »

  1.   

    --用group by分组查询就行了:Select 台帐编号,煤炭品种,存储方式,sum(库存数量) as 库存数量
    from 表
    group by 台帐编号,煤炭品种,存储方式
      

  2.   

    阿来和VINSONSHEN没明白我的意思,我说的是行列转换的问题!
      

  3.   

    create table #t1(台帐编号 nvarchar(20),班次 nvarchar(2),煤炭品种 nvarchar(20),存储方式 nvarchar(1),库存数量 int)insert into #t1 values('C020120040101','01','L010101','1',1000)
    insert into #t1 values('C020120040101','01','L010102','1',1000)
    insert into #t1 values('C020120040101','01','L010103','1',1000)
    insert into #t1 values('C020120040101','01','L010201','1',1000)
    insert into #t1 values('C020120040101','01','L010203','1',1000)
    insert into #t1 values('C020120040101','01','L020201','1',1000)
    insert into #t1 values('C020120040101','01','L020201','2',1000)
    insert into #t1 values('C020120040101','02','L010101','1',900)
    insert into #t1 values('C020120040101','02','L010102','1',900)
    insert into #t1 values('C020120040101','02','L010103','1',900)
    insert into #t1 values('C020120040101','02','L010201','1',900)
    insert into #t1 values('C020120040101','02','L010203','1',900)
    insert into #t1 values('C020120040101','02','L020201','1',900)
    insert into #t1 values('C020120040101','02','L020201','2',900)
    insert into #t1 values('C020120040101','03','L010101','1',800)
    insert into #t1 values('C020120040101','03','L010102','1',800)
    insert into #t1 values('C020120040101','03','L010103','1',800)
    insert into #t1 values('C020120040101','03','L010201','1',800)
    insert into #t1 values('C020120040101','03','L010203','1',800)
    insert into #t1 values('C020120040101','03','L020201','1',800)
    insert into #t1 values('C020120040101','03','L020201','2',800)
    declare @str nvarchar(4000)
    select @str=''
    select @str=@str+',[煤炭品种为('+煤炭品种+'+存储方式为)(1)]=isnull((select sum(1) from #t1 where 煤炭品种='''+煤炭品种+''' and 存储方式=''1''),0)'
    +',[煤炭品种为('+煤炭品种+'+存储方式为)(2)]=isnull((select sum(1) from #t1 where 煤炭品种='''+煤炭品种+''' and 存储方式=''2''),0)'
    from (select distinct 煤炭品种 from #t1) a
    select @str='select 台帐编号'+@str+' from #t1 group by 台帐编号 '
    exec( @str)
    drop table #t1--测试结果
    --C020120040101 3 0 3 0 3 0 3 0 3 0 3 3
      

  4.   

    declare @str nvarchar(4000)
    是求库存数量,修改一下。
    select @str=''
    select @str=@str+',[煤炭品种为('+煤炭品种+'+存储方式为)(1)]=isnull((select sum(库存数量) from #t1 where 煤炭品种='''+煤炭品种+''' and 存储方式=''1''),0)'
    +',[煤炭品种为('+煤炭品种+'+存储方式为)(2)]=isnull((select sum(1) from #t1 where 煤炭品种='''+煤炭品种+''' and 存储方式=''2''),0)'
    from (select distinct 煤炭品种 from #t1) a
    select @str='select 台帐编号'+@str+' from #t1 group by 台帐编号 '
    exec( @str)
      

  5.   

    create table #t1(台帐编号 nvarchar(20),班次 nvarchar(2),煤炭品种 nvarchar(20),存储方式 nvarchar(1),库存数量 int)insert into #t1 values('C020120040101','01','L010101','1',1000)
    insert into #t1 values('C020120040101','01','L010102','1',1000)
    insert into #t1 values('C020120040101','01','L010103','1',1000)
    insert into #t1 values('C020120040101','01','L010201','1',1000)
    insert into #t1 values('C020120040101','01','L010203','1',1000)
    insert into #t1 values('C020120040101','01','L020201','1',1000)
    insert into #t1 values('C020120040101','01','L020201','2',1000)
    insert into #t1 values('C020120040101','02','L010101','1',900)
    insert into #t1 values('C020120040101','02','L010102','1',900)
    insert into #t1 values('C020120040101','02','L010103','1',900)
    insert into #t1 values('C020120040101','02','L010201','1',900)
    insert into #t1 values('C020120040101','02','L010203','1',900)
    insert into #t1 values('C020120040101','02','L020201','1',900)
    insert into #t1 values('C020120040101','02','L020201','2',900)
    insert into #t1 values('C020120040101','03','L010101','1',800)
    insert into #t1 values('C020120040101','03','L010102','1',800)
    insert into #t1 values('C020120040101','03','L010103','1',800)
    insert into #t1 values('C020120040101','03','L010201','1',800)
    insert into #t1 values('C020120040101','03','L010203','1',800)
    insert into #t1 values('C020120040101','03','L020201','1',800)
    insert into #t1 values('C020120040101','03','L020201','2',800)
    insert into #t1 values('C020120040102','03','L020201','1',800)
    insert into #t1 values('C020120040102','03','L020202','2',800)
    insert into #t1 values('C020120040102','03','L020203','1',800)
    insert into #t1 values('C020120040102','03','L020201','2',800)
    insert into #t1 values('C020120040102','03','L020202','1',800)
    insert into #t1 values('C020120040102','03','L020203','2',800)
    insert into #t1 values('C020120040102','03','L020201','1',800)declare @str nvarchar(4000)
    select @str=''
    select @str=@str+',[煤炭品种为('+煤炭品种+'+存储方式为)(1)]=isnull((select sum(库存数量) from #t1 where 煤炭品种='''+煤炭品种+''' and 存储方式=''1'' and 台帐编号=b.台帐编号),0)'
    +',[煤炭品种为('+煤炭品种+'+存储方式为)(2)]=isnull((select sum(库存数量) from #t1 where 煤炭品种='''+煤炭品种+''' and 存储方式=''2'' and 台帐编号=b.台帐编号),0)'
    from (select distinct 煤炭品种 from #t1) a
    select @str='select 台帐编号'+@str+' from #t1 b group by 台帐编号 'exec( @str)drop table #t1
      

  6.   

    注意:nvarchar类型最大为4000个字符,如果煤炭品种太多,动态查询将超过这个限制。
      

  7.   

    收到!非常感谢zh_zh_y(决不放弃)!这个问题已经费了我两天时间!
    接分!