So, I feel the need to be recorded, so that after the encounter this problem again, and get answers from the idea.
Neighborhoods: I have a table has storage time, there is a library time, I want the style number of monthly sales and inventory history
http://bbs.csdn.net/topics/390629790
library structure something like this:
Item No. style timespan leaving time
A001 2011-1-10 10
B002 10 2011-1-10 2011-2-1
C003 10 2012 2012-2-2 -1-15
D004 10 2013-2-3 2013-1-5
E005 10 2013-2-3
F006 2011-2-15 2011-3-16 15
G007 15 2012-3 2011-2-16 -16
H009 2013-1-10 15
M012 18 is 2011-1-4
C009 18 is 2011-4-5 2012-5-6
F008 18 is 2012-2-19 2013-1-1
E008 18 is 2013-1-5 2013-2 -6
I want to get results:
the number of inventory items style No. date sales part
10 2011-2 1 1
10 2012-2 1 1
10 2 1 2013-1
15 2011-3 1 0
15 2012-3 1 1
18 2012-5 1 1
18 2013-1 1 1
18 2013-2 1 1
My solution:
-
if object_id('tb') is not null
-
drop
table tb
-
go
-
-
create
table tb
-
(
-
[货号]
varchar(
20),[样式号]
int,
-
[入库时间] datetime,
-
[出库时间] datetime
-
)
-
-
insert
into tb
-
SELECT
'a001',
10,
'2011-01-10',
null
UNION ALL
-
SELECT
'b002',
10,
'2011-01-10',
'2011-02-01'
UNION ALL
-
SELECT
'c003',
10,
'2012-01-15',
'2012-02-02'
UNION ALL
-
SELECT
'd004',
10,
'2013-01-03',
'2013-01-05'
UNION ALL
-
SELECT
'e005',
10,
'2013-01-03',
null
UNION ALL
-
SELECT
'f006',
15,
'2011-02-15',
'2011-03-16'
UNION ALL
-
SELECT
'g007',
15,
'2011-02-16',
'2012-03-16'
UNION ALL
-
SELECT
'h009',
15,
'2013-01-10',
null
UNION ALL
-
SELECT
'm012',
18,
'2011-01-04',
null
UNION ALL
-
SELECT
'c009',
18,
'2011-04-05',
'2012-05-06'
UNION ALL
-
SELECT
'f008',
18,
'2012-02-19',
'2013-01-01'
UNION ALL
-
SELECT
'e008',
18,
'2013-01-05',
'2013-02-06'
-
go
-
-
-
;with t
-
as(
-
select *,
-
row_number()
over(
partition
by 样式号
-
order
by 入库时间,出库时间)
as
rownum
-
from tb
-
),
-
-
tt
-
as
-
(
-
select *,
-
case
when 出库时间
is
null
-
then (
select top
1 出库时间
-
from t t2
-
where t1.样式号 = t2.样式号
and
-
t1.rownum > t2.rownum
-
order
by t2.rownum
desc)
-
else 出库时间
-
end
as prior_row,
-
-
case
when 出库时间
is
null
-
then (
select top
1 出库时间
-
from t t2
-
where t1.样式号 = t2.样式号
and
-
t1.rownum < t2.rownum
-
order
by t2.rownum )
-
else 出库时间
-
end
as next_row
-
-
from t t1
-
),
-
-
ttt
-
as
-
(
-
select 样式号,
-
convert(
varchar(
7),
isnull(next_row,prior_row),
120)
as 日期,
-
count(出库时间) 销售件数,
-
count(入库时间) 库存件数,
-
count(入库时间) -
count(出库时间) 剩余库存
-
--row_number() over(partition by 样式号
-
--order by convert(varchar(7),isnull(next_row,prior_row),120)) as rownum
-
from tt
-
group
by 样式号,
-
convert(
varchar(
7),
isnull(next_row,prior_row),
120)
-
-
)
-
-
select t1.样式号,t1.日期,
-
isnull(t1.销售件数,
0)
as 销售件数 ,
-
-
isnull(t1.库存件数,
0) +
-
isnull((
select
sum(库存件数)-
sum(销售件数)
as 剩余库存
from ttt t2
-
where t2.样式号 = t1.样式号
-
and t2.日期 < t1.日期
-
),
0) -
-
isnull(t1.销售件数,
0)
as 库存件数
-
-
from ttt t1
-
order
by t1.样式号
-
/*
-
样式号 日期 销售件数 库存件数
-
10 2011-02 1 1
-
10 2012-02 1 1
-
10 2013-01 1 2
-
15 2011-03 1 0
-
15 2012-03 1 1
-
18 2012-05 1 1
-
18 2013-01 1 1
-
18 2013-02 1 1
-
*/