邹建大侠快来帮忙呀!

sxy1324 2004-12-09 04:54:53
是行列转换的问题!

台帐编号 班次 煤炭品种 存储方式 库存数量
C020120040101 01 L010101 1 1000
C020120040101 01 L010102 1 1000
C020120040101 01 L010103 1 1000
C020120040101 01 L010201 1 1000
C020120040101 01 L010203 1 1000
C020120040101 01 L020201 1 1000
C020120040101 01 L020201 2 1000
C020120040101 02 L010101 1 900
C020120040101 02 L010102 1 900
C020120040101 02 L010103 1 900
C020120040101 02 L010201 1 900
C020120040101 02 L010203 1 900
C020120040101 02 L020201 1 900
C020120040101 02 L020201 2 900
C020120040101 03 L010101 1 800
C020120040101 03 L010102 1 800
C020120040101 03 L010103 1 800
C020120040101 03 L010201 1 800
C020120040101 03 L010203 1 800
C020120040101 03 L020201 1 800
C020120040101 03 L020201 2 800

煤炭品种是动态的,存储方式只有1和2两种,我想得到以下表:

台帐编号,(煤炭品种为(L010101)+存储方式为(1))时的库存数量值,(煤炭品种为(L010101)+存储方式为(2))时的库存数量值,(煤炭品种为(L010102)+存储方式为(1))时的库存数量值,(煤炭品种为(L010102)+存储方式为(1))时的库存数量值.........

我是新手,希望邹大侠和各位高手一定要帮我这个忙,我在这里先谢谢大家了!


...全文
103 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
sxy1324 2004-12-10
  • 打赏
  • 举报
回复
收到!非常感谢zh_zh_y(决不放弃)!这个问题已经费了我两天时间!
接分!
zh_zh_y 2004-12-10
  • 打赏
  • 举报
回复
注意:nvarchar类型最大为4000个字符,如果煤炭品种太多,动态查询将超过这个限制。
zh_zh_y 2004-12-10
  • 打赏
  • 举报
回复
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
sxy1324 2004-12-10
  • 打赏
  • 举报
回复
我试了,结果正确,但是如果台帐编号为“C020120040102”,“C020120040103”........还有记录,我要按台帐编号统计,怎么办?
我自己试了一下,结果是所有台帐编号的统计值
zh_zh_y 2004-12-10
  • 打赏
  • 举报
回复
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)
zh_zh_y 2004-12-10
  • 打赏
  • 举报
回复
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
sxy1324 2004-12-09
  • 打赏
  • 举报
回复
阿来和VINSONSHEN没明白我的意思,我说的是行列转换的问题!
vinsonshen 2004-12-09
  • 打赏
  • 举报
回复
--用group by分组查询就行了:

Select 台帐编号,煤炭品种,存储方式,sum(库存数量) as 库存数量
from 表
group by 台帐编号,煤炭品种,存储方式
WangZWang 2004-12-09
  • 打赏
  • 举报
回复
Select 台帐编号,煤炭品种,存储方式,sum(库存数量) as 库存数量
from 表名
group by 台帐编号,煤炭品种,存储方式

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧