@@@@@@@@@@@@@@@@@@SQL查询结果的显示@@@@@@@@@@@@@@@@@@@@@@

share1011 2006-04-19 09:18:07
我的一个表中有
仓库编号,仓库名称,产品编号,产品数量
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
...全文
125 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
撸大湿 2006-04-19
  • 打赏
  • 举报
回复
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',60



declare @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
share1011 2006-04-19
  • 打赏
  • 举报
回复
谢谢,2005是没有的,只有7.0 版的:)
云中客 2006-04-19
  • 打赏
  • 举报
回复
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)

zjcxc 元老 2006-04-19
  • 打赏
  • 举报
回复
-- SQL 2005

SELECT *
FROM(
SELECT 仓库编号, 仓库名称, 产品编号, 产品数量 FROM tb
)DATA
PIVOT(
SUM(产品数量)
FOR 产品编号 IN([1000], [1001], [1003], [1100], [1111], [1020])
)P
xeqtr1982 2006-04-19
  • 打赏
  • 举报
回复
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 ,60

declare @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
zjcxc 元老 2006-04-19
  • 打赏
  • 举报
回复
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 仓库编号, 仓库名称
xiaoku 2006-04-19
  • 打赏
  • 举报
回复
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)

34,576

社区成员

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

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