SQL 多表查询问题求大神指点
例如有一个入库表,出库表,销售表,
入库表
ID 品名 入库数量 入库时间
1 矿泉水 100 2013-01-02
2 方便面 60 2013-01-03
3 方便面 50 2013-01-03
4 矿泉水 80 2013-01-04
5 方便面 50 2013-01-05
销售表
ID 品名 销售数量 销售时间
1 矿泉水 5 2013-01-03
2 矿泉水 10 2013-01-04
出库表
ID 品名 出库数量 出库时间
1 方便面 50 2013-01-03
2 矿泉水 80 2013-01-04
然后我要得到的信息是2013年1月五号之前的商品在库数量
品名 在库数量
矿泉水 85
方便面 60
SQL C#
[解决办法]
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-11-21 07:47:38
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
--Dec 28 2012 20:23:12
--Copyright (c) Microsoft Corporation
--Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[入库表]
if object_id('[入库表]') is not null drop table [入库表]
go
create table [入库表]([ID] int,[品名] varchar(6),[入库数量] int,[入库时间] datetime)
insert [入库表]
select 1,'矿泉水',100,'2013-01-02' union all
select 2,'方便面',60,'2013-01-03' union all
select 3,'方便面',50,'2013-01-03' union all
select 4,'矿泉水',80,'2013-01-04' union all
select 5,'方便面',50,'2013-01-05'
--> 测试数据:[销售表]
if object_id('[销售表]') is not null drop table [销售表]
go
create table [销售表]([ID] int,[品名] varchar(6),[销售数量] int,[销售时间] datetime)
insert [销售表]
select 1,'矿泉水',5,'2013-01-03' union all
select 2,'矿泉水',10,'2013-01-04'
--> 测试数据:[出库表]
if object_id('[出库表]') is not null drop table [出库表]
go
create table [出库表]([ID] int,[品名] varchar(6),[出库数量] int,[出库时间] datetime)
insert [出库表]
select 1,'方便面',50,'2013-01-03' union all
select 2,'矿泉水',80,'2013-01-04'
--------------开始查询--------------------------
DECLARE @d DATETIME
SET @d='2013-01-05'--统计日期
SELECT [品名],SUM([入库数量])在库数量
FROM(
select [品名],SUM([入库数量])[入库数量] from [入库表]
WHERE [入库时间]<@d
GROUP BY [品名]
UNION ALL
select [品名],-1*SUM([销售数量])[入库数量] from [销售表]
WHERE [销售时间] <@d
GROUP BY [品名]
UNION ALL
select [品名],-1*SUM([出库数量])[入库数量] from [出库表]
WHERE [出库时间] <@d
GROUP BY [品名]
)a
GROUP BY [品名]
----------------结果----------------------------
/*
品名 在库数量
------ -----------
方便面 60
矿泉水 85
*/
----------------------------------------------------------------
-- Author :TravyLee(走自己的路,让狗去叫吧!)
-- Date :2013-11-21 09:38:30
-- Version:
-- Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
--Jul 9 2008 14:43:34
--Copyright (c) 1988-2008 Microsoft Corporation
--Developer Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:#rkb
if object_id('tempdb.dbo.#rkb') is not null drop table #rkb
go
create table #rkb([ID] int,[品名] varchar(6),[入库数量] int,[入库时间] datetime)
insert #rkb
select 1,'矿泉水',100,'2013-01-02' union all
select 2,'方便面',60,'2013-01-03' union all
select 3,'方便面',50,'2013-01-03' union all
select 4,'矿泉水',80,'2013-01-04' union all
select 5,'方便面',50,'2013-01-05'
--> 测试数据:#xsb
if object_id('tempdb.dbo.#xsb') is not null drop table #xsb
go
create table #xsb([ID] int,[品名] varchar(6),[销售数量] int,[销售时间] datetime)
insert #xsb
select 1,'矿泉水',5,'2013-01-03' union all
select 2,'矿泉水',10,'2013-01-04'
--> 测试数据:#xsb
if object_id('tempdb.dbo.#ckb') is not null drop table #ckb
go
create table #ckb([ID] int,[品名] varchar(6),[出库数量] int,[出库时间] datetime)
insert #ckb
select 1,'方便面',50,'2013-01-03' union all
select 2,'矿泉水',80,'2013-01-04'
go
;with t
as
(
select
[品名],SUM([入库数量]) as [入库数量] from #rkb
where [入库时间]<='2013-01-04'
group by [品名]
),
m as
(
select
[品名],SUM([销售数量]) as [销售数量] from #xsb
where [销售时间]<='2013-01-04'
group by [品名]
),n
as
(
select
[品名],SUM([出库数量]) as [出库数量] from #ckb
where [出库时间]<='2013-01-04'
group by [品名]
)
select
t.[品名],t.入库数量-isnull(m.销售数量,0)-isnull(n.出库数量,0) from t
left join m on t.品名=m.品名
left join n on t.品名=n.品名