求一3表查询 其中一表行转列

lu仙深 2014-05-26 05:34:22




商品表
商品ID 商品名称
101 花生
102 啤酒

仓库表 仓库会添加的
仓库ID 仓库名称
201 仓1
202 仓2

库存表
商品ID 仓库ID 商品数量
101 201 100
101 202 50
102 201 80

库存表
商品ID 商品名称 仓1数 仓2数 合计
101 花生 100 50 150
102 啤酒 80 0 80


求高手,以及过路的大侠,帮帮忙
谢谢
...全文
95 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lu仙深 2014-08-05
  • 打赏
  • 举报
回复
发现一个问题,没有库存的,就无法显示
lu仙深 2014-08-05
  • 打赏
  • 举报
回复
试下,结贴了还能回复不
lu仙深 2014-05-26
  • 打赏
  • 举报
回复
小弟今天有幸遇见2位版主为我解围,非常感谢 好好感动
唐诗三百首 2014-05-26
  • 打赏
  • 举报
回复

create table 商品表	
(商品ID int,商品名称 varchar(10))

insert into 商品表
 select 101,'花生' union all
 select 102,'啤酒'	

create table 仓库表
(仓库ID int,仓库名称 varchar(10))

insert into 仓库表
 select 201,'仓1' union all	
 select 202,'仓2	'

create table 库存表	
(商品ID int,仓库ID int,商品数量 int)

insert into 库存表
 select 101,201,100 union all
 select 101,202,50 union all
 select 102,201,80


declare @tsql varchar(6000)

select @tsql=isnull(@tsql+',','')
            +'sum(case when 仓库名称='''+仓库名称+''' then 商品数量 else 0 end) '''+仓库名称+'数'' '
 from 仓库表

select @tsql='
select 商品ID,
       商品名称,'+@tsql+',
       sum(商品数量) ''合计''
from
(select a.商品ID,c.商品名称,b.仓库名称,a.商品数量
 from 库存表 a
 inner join 仓库表 b on a.仓库ID=b.仓库ID
 inner join 商品表 c on a.商品ID=c.商品ID) a
group by 商品ID,商品名称 '

exec(@tsql)

/*
商品ID        商品名称       仓1数         仓2数        合计
----------- ---------- ----------- ----------- -----------
101         花生         100         50          150
102         啤酒         80          0           80

(2 row(s) affected)
*/
發糞塗牆 2014-05-26
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author  :DBA_HuangZJ(发粪涂墙)
-- Date    :2014-05-26 17:37:39
-- Version:
--      Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64) 
--	Apr  2 2010 15:48:46 
--	Copyright (c) Microsoft Corporation
--	Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
----> 测试数据[商品表]
--if object_id('商品表') is not null drop table 商品表
--go 
--create table 商品表([商品ID] int,[商品名称] nvarchar(4))
--insert 商品表
--select 101,N'花生' union all
--select 102,N'啤酒'
----> 测试数据[仓库表]
--if object_id('仓库表') is not null drop table 仓库表
--go 
--create table 仓库表([仓库ID] int,[仓库名称] nvarchar(4))
--insert 仓库表
--select 201,N'仓1' union all
--select 202,N'仓2'
----> 测试数据[库存表]
--if object_id('库存表') is not null drop table 库存表
--go 
--create table 库存表([商品ID] int,[仓库ID] int,[商品数量] int)
--insert 库存表
--select 101,201,100 union all
--select 101,202,50 union all
--select 102,201,80
--------------生成数据--------------------------
IF OBJECT_ID('TempDB..#t','u')IS NOT NULL 
DROP TABLE #t

select c.商品ID ,c.商品名称,b.仓库名称,a.商品数量 INTO #t
from 库存表 a INNER JOIN 仓库表 b ON a.仓库ID=b.仓库ID
INNER JOIN 商品表 c ON a.商品ID=c.商品ID

declare @s nvarchar(4000)
set @s=''
Select     @s=@s+','+quotename(仓库名称)+N'=sum(case when [仓库名称]=N'+quotename(仓库名称,'''')+N' then [商品数量] else 0 end)'
from #t group by 仓库名称
exec(N'select [商品ID],商品名称'+@s+N',[合计]=sum(商品数量) from #t group by [商品ID],商品名称 order by 商品ID')

----------------结果----------------------------
/*
商品ID        商品名称 仓1          仓2          合计
----------- ---- ----------- ----------- -----------
101         花生   100         50          150
102         啤酒   80          0           80
 
*/

34,838

社区成员

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

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