交叉表、行转列、合计问题请教!~

am2000 2003-10-21 12:27:47
年度 月份 货号 大小 库存
2003 10 3316-1 22 0
2003 10 3316-1 23 19
2003 10 3316-1 24 20
2003 10 3316-1 25 36
2003 10 3316-1 26 34
2003 10 3316-1 27 4
2003 10 3316-1 28 15
2003 10 3316-1 29 3


显示为:
年度 月份 货号 22 23 24 25 26 27 28 29 合计
2003 10 3316-1 0 19 20 36 34 4 15 3 121

其中大小字段是存在变化的。
我该如何实现?
谢谢!
...全文
68 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
pengdali 2003-10-21
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql='select 年度,月份,货号'
select @sql=@sql+',sum(case 大小 when '''+cast(大小 as varchar(10))+''' then 库存 else 0 end) ['+大小+']'
from (select distinct 大小 from tablename) a
select @sql=@sql+',sum(库存) 合计 from tablename group by 年度,月份,货号'
exec(@sql)
sdhdy 2003-10-21
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql='select 年度,月份,货号'
select @sql=@sql+',sum(case 大小 when '+大小+' then 库存 else 0 end) ['+大小+']'
from (select distinct 大小 from tablename) a
select @sql=@sql+',sum(库存) as 合计 from tablename group by 年度,月份,货号'
exec(@sql)
sdhdy 2003-10-21
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql='select 年度,月份,货号'
select @sql=@sql+',sum(case 大小 when '''+大小+''' then 库存 else 0 end) ['+大小+']'
from (select distinct 大小 from tablename) a
select @sql=@sql+',sum(库存) as 合计 from tablename group by 年度,月份,货号'
exec(@sql)
zxlkxy 2003-10-21
  • 打赏
  • 举报
回复
pengdali(大力 V3.0):我知道了,是我的Sql超过8000字节。
我的问题有点不同,我的大小要取一部份,位置是定的:
2003 10 3316-1 A22 0
2003 10 3316-1 A23 19
2003 10 3316-1 A24 20
2003 10 3316-1 A25 36
2003 10 3316-2 B22 34
2003 10 3316-2 B23 4
2003 10 3316-2 B24 15
2003 10 3316-2 B26 3
年度 月份 货号 22 23 24 25 26 合计
2003 10 3316-1 0 19 20 36 0 75
2003 10 3316-2 34 4 15 0 3 56
pengdali 2003-10-21
  • 打赏
  • 举报
回复
你的代码?
zxlkxy 2003-10-21
  • 打赏
  • 举报
回复
pengdali(大力 V3.0): 第 1 行: 'when' 附近有语法错误。
zxlkxy 2003-10-21
  • 打赏
  • 举报
回复
第 1 行: 'when' 附近有语法错误。
pengdali 2003-10-21
  • 打赏
  • 举报
回复
你只要把中文部分换成你的列名就可以了。
qdubit 2003-10-21
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql='select 年度,月份,货号'
select @sql=@sql+',sum(case 大小 when '''+cast(大小 as varchar(10))+''' then 库存 else 0 end) ['+大小+']'
from (select distinct 大小 from tablename) a
select @sql=@sql+',sum(库存) 合计 from tablename group by 年度,月份,货号'
exec(@sql)
zxlkxy 2003-10-21
  • 打赏
  • 举报
回复
看不懂,怎么办呀!我也要这种查询,
pengdali 2003-10-21
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql='select 年度,月份,货号'
select @sql=@sql+',sum(case 大小 when '''+cast(大小 as varchar(10))+''' then 库存 else 0 end) ['+cast(大小 as varchar(10))+']'
from (select distinct 大小 from tablename) a
select @sql=@sql+',sum(库存) 合计 from tablename group by 年度,月份,货号'
exec(@sql)
aierong 2003-10-21
  • 打赏
  • 举报
回复
带合计的

declare @sql varchar(8000)
select @sql='select 年度,月份,货号,'
select @sql=@sql+'sum(case when 大小='+dx+' then 库存 end) ['+dx+'],'
from (select distinct convert(varchar(1000),大小) dx from #ai) a
select @sql=@sql+'sum(库存) 合计 from #ai group by 年度,月份,货号'
print @sql
execute (@sql)
aierong 2003-10-21
  • 打赏
  • 举报
回复
年度 月份 货号 大小 库存
2003 10 3316-1 22 0
2003 10 3316-1 23 19
2003 10 3316-1 24 20
2003 10 3316-1 25 36
2003 10 3316-1 26 34
2003 10 3316-1 27 4
2003 10 3316-1 28 15
2003 10 3316-1 29 3


显示为:
年度 月份 货号 22 23 24 25 26 27 28 29 合计
2003 10 3316-1 0 19 20 36 34 4 15 3 121

其中大小字段是存在变化的。
我该如何实现?
谢谢!


drop table #ai

create table #ai(年度 int, 月份 tinyint ,货号 varchar(1000), 大小 int, 库存 int)
insert into #ai select 2003, 10, '3316-1', 22 ,0
insert into #ai select 2003, 10, '3316-1', 23 ,19
insert into #ai select 2003, 10, '3316-1' , 24 , 20
insert into #ai select 2003, 10, '3316-1' , 25 , 36
insert into #ai select 2003, 10, '3316-1' , 26 , 34
insert into #ai select 2003, 10, '3316-1' , 27 ,4
insert into #ai select 2003, 10, '3316-1' , 28 , 15
insert into #ai select 2003, 10, '3316-1' , 29, 3
go
declare @sql varchar(8000)
select @sql='select 年度,月份,货号,'
select @sql=@sql+'sum(case when 大小='+dx+' then 库存 end) ['+dx+'],'
from (select distinct convert(varchar(1000),大小) dx from #ai) a
select @sql=left(@sql,len(@sql)-1)
select @sql=@sql+' from #ai group by 年度,月份,货号'
print @sql
execute (@sql)


22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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