34,587
社区成员
发帖
与我相关
我的任务
分享
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'
go
DECLARE @d DATETIME
SET @d='2013-01-05'--统计日期
select a.品名,ISNULL(a.v,0) - ISNULL(b.v,0) - ISNULL(c.v,0) 在库数量
from
(
select [品名],SUM([入库数量]) as v
from [入库表]
where 入库时间 < @d
group by [品名]
)a
left join
(
select [品名],SUM([销售数量]) as v
from [销售表]
where 销售时间 < @d
group by [品名]
)b
on a.品名 = b.品名
left join
(
select [品名],SUM([出库数量]) as v
from [出库表]
where 出库时间 < @d
group by [品名]
)c
on a.品名 = c.品名
/*
品名 在库数量
方便面 60
矿泉水 85
*/
select a.品名,a.数量-b.数量 from(
select 品名,SUM(入库数量)'数量'
from [入库表]
where convert(varchar(10),入库时间,120) < '2013-01-05'
group by 品名)a,
(
select 品名, SUM(数量)'数量' from
(
select 品名,SUM(销售数量)'数量'
from [销售表]
where convert(varchar(10),销售时间,120) < '2013-01-05'
group by 品名
union all
select 品名,SUM(出库数量)'数量'
from [出库表]
where convert(varchar(10),出库时间,120) <'2013-01-05'
group by 品名
)a
group by a.品名
)b
where a.品名=b.品名
----------------------------------------------------------------
-- 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.品名
----------------------------------------------------------------
-- 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
*/
if object_id('ins') is not null drop table ins
go
create table ins([ID] int,[品名] varchar(6),[入库数量] int,[入库时间] datetime)
insert ins
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'
go
if object_id('sales') is not null drop table sales
go
create table sales([ID] int,[品名] varchar(6),[销售数量] int,[销售时间] datetime)
insert sales
select 1,'矿泉水',5,'2013-01-03' union all
select 2,'矿泉水',10,'2013-01-04'
go
if object_id('chu') is not null drop table chu
go
create table chu([ID] int,[品名] varchar(6),[出库数量] int,[出库时间] datetime)
insert chu
select 1,'方便面',50,'2013-01-03' union all
select 2,'矿泉水',80,'2013-01-04'
go
SELECT a.[品名],(ISNULL(a.[入库数量],0)-ISNULL(b.[销售数量],0)-ISNULL(c.[出库数量],0)) FROM (
SELECT SUM([入库数量]) AS [入库数量],[品名] FROM ins
WHERE CONVERT(CHAR(10),[入库时间],120) < '2013-01-05'
GROUP BY [品名]) AS a
LEFT JOIN (
SELECT SUM([销售数量]) AS [销售数量],[品名] FROM sales
WHERE CONVERT(CHAR(10),[销售时间],120) < '2013-01-05'
GROUP BY [品名]) AS b
ON a.[品名] = b.[品名]
LEFT JOIN (
SELECT SUM([出库数量]) AS [出库数量],[品名] FROM chu
WHERE CONVERT(CHAR(10),[出库时间],120) < '2013-01-05'
GROUP BY [品名]) c
ON a.[品名]= c.[品名]
ORDER BY a.[品名] DESC