当前位置: 代码迷 >> Sql Server >> 求13表查询 其中一表行转列
  详细解决方案

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

热度:62   发布时间:2016-04-24 10:39:24.0
求一3表查询 其中一表行转列




商品表
商品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


求高手,以及过路的大侠,帮帮忙 
谢谢
------解决方案--------------------
----------------------------------------------------------------
-- 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
 
*/

------解决方案--------------------

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
  相关解决方案