原表如下:
账号 存取日期 借方 贷方 余额
A 2014-10-6 0 100000 100000
A 2014-10-9 20000 0 80000
A 2014-10-11 0 50000 130000
A 2014-11-7 20000 0 110000
A 2014-11-7 0 50000 160000
A 2014-11-15 50000 0 110000
10月分补录其他每天余额
账号 存取日期 借方 贷方 余额
A 2014-10-1 0 0 0(补录)
A 2014-10-2 0 0 0(补录)
A 2014-10-3 0 0 0(补录)
A 2014-10-4 0 0 0(补录)
A 2014-10-5 0 0 0(补录)
A 2014-10-6 0 100000 100000
A 2014-10-9 20000 0 80000
A 2014-10-11 0 50000 130000
A 2014-10-12 0 0 130000(补录)
A 2014-10-13 0 0 130000(补录)
...................
A 2014-10-31 0 0 130000(补录)
11月分补录其他每天余额
A 2014-11-01 0 0 130000(补录)
A 2014-11-02 0 0 130000(补录)
...................
A 2014-11-06 0 0 130000(补录)
A 2014-11-7 20000 0 110000
A 2014-11-7 0 50000 160000
A 2014-11-8 0 0 160000(补录)
A 2014-11-9 0 0 160000(补录)
...................
A 2014-11-14 0 0 160000(补录)
A 2014-11-15 50000 0 110000
A 2014-11-16 0 0 110000(补录)
A 2014-11-17 0 0 110000(补录)
。。。。。。。。。。。
A 2014-11-30 0 0 110000(补录)
------解决思路----------------------
-- 你自己再调整一下吧
create table test(Ano varchar(10) , adate date,jie int , dai int , ye int)
go
insert into test values
('A','2014-10-6' , 0 , 100000, 100000),
('A','2014-10-9' , 20000, 0 , 80000 ),
('A','2014-10-11', 0 , 50000 , 130000),
('A','2014-11-7' , 20000, 0 , 110000),
('A','2014-11-7' , 0 , 50000 , 160000),
('A','2014-11-15', 50000, 0 , 110000)
go
select * from test
go
declare @s_day date = '2014-10-01', @e_day date = '2014-11-30'
select xdate ,
isnull((select top 1 ye from test where x.xdate>test.adate order by adate desc),0)
from (
select DATEADD(day,number, @s_day ) xdate
from master..spt_values
where type ='p' and number<= DATEDIFF(day,@s_day,@e_day)
except
select adate from test where Ano = 'A'
)x
go
drop table test
go
(6 行受影响)
Ano adate jie dai ye
---------- ---------- ----------- ----------- -----------
A 2014-10-06 0 100000 100000
A 2014-10-09 20000 0 80000
A 2014-10-11 0 50000 130000
A 2014-11-07 20000 0 110000
A 2014-11-07 0 50000 160000
A 2014-11-15 50000 0 110000
(6 行受影响)
xdate
---------- -----------
2014-10-01 0
2014-10-02 0
2014-10-03 0
2014-10-04 0
2014-10-05 0
2014-10-07 100000
2014-10-08 100000
2014-10-10 80000
2014-10-12 130000
2014-10-13 130000
2014-10-14 130000
2014-10-15 130000
2014-10-16 130000
2014-10-17 130000
2014-10-18 130000
2014-10-19 130000
2014-10-20 130000
2014-10-21 130000
2014-10-22 130000
2014-10-23 130000
2014-10-24 130000
2014-10-25 130000
2014-10-26 130000
2014-10-27 130000
2014-10-28 130000
2014-10-29 130000
2014-10-30 130000
2014-10-31 130000
2014-11-01 130000
2014-11-02 130000
2014-11-03 130000
2014-11-04 130000
2014-11-05 130000
2014-11-06 130000
2014-11-08 110000
2014-11-09 110000
2014-11-10 110000
2014-11-11 110000
2014-11-12 110000
2014-11-13 110000
2014-11-14 110000
2014-11-16 110000
2014-11-17 110000
2014-11-18 110000
2014-11-19 110000
2014-11-20 110000
2014-11-21 110000
2014-11-22 110000
2014-11-23 110000
2014-11-24 110000
2014-11-25 110000
2014-11-26 110000
2014-11-27 110000
2014-11-28 110000
2014-11-29 110000
2014-11-30 110000
(56 行受影响)
------解决思路----------------------
declare @FirstDate date='2014-10-1';
declare @LastDate date='2014-11-30';
with t(账号,存取日期,借方,贷方,余额) as (
select 'A','2014-10-6','0','100000','100000' union all
select 'A','2014-10-9','20000','0','80000' union all
select 'A','2014-10-11','0','50000','130000' union all
select 'A','2014-11-7','20000','0','110000' union all
select 'A','2014-11-7','0','50000','160000' union all
select 'A','2014-11-15','50000','0','110000' union all
select 'B','2014-10-15','0','50000','50000' --加一行例外数据
)
,tt(账号,存取日期,借方,贷方,余额) as (
select 账号,@FirstDate,0,0,0 from t group by 账号
union all
select * from t
)
,tAcc(账号) as (select 账号 from tt group by 账号)
,ttt(账号,存取日期,借方,贷方,余额,rn) as (
select *
--用存取日期其实不严谨,因为你的日期可能重复,如11-7,你有两条,所以要换成一个真正顺序的字段,如ID。
,rn=ROW_NUMBER()over(partition by 账号 order by 存取日期)
from tt
)
,list as (
select number from master..spt_values where type='p'
)
select tacc.账号,DATEADD(DAY,number,@FirstDate) 存取日期
,case when ttt.账号 is null then 0 else ttt.借方 end 借方
,case when ttt.账号 is null then 0 else ttt.贷方 end 贷方
,tttt.余额
from tAcc
outer apply (select * from list where @LastDate>=DATEADD(DAY,number,@FirstDate)) l
outer apply (select top 1 * from ttt where ttt.账号=tAcc.账号 and ttt.存取日期<=DATEADD(DAY,number,@FirstDate) order by rn desc) tttt
left join ttt on ttt.账号=tAcc.账号 and ttt.存取日期=DATEADD(DAY,number,@FirstDate)