假设人力资源部下发1月份奖励费用如下表:月份 编号 名字 奖励1 奖励2 奖励3 总共
201201 001 李四 1000 1000 1000 3000
201201 002 张三 500 500 500 1500
导入系统后如下表:月份 编号 名字 可报数 已报数 余额
201201 001 李四 3000 0
201201 002 张三 1500 0
-----------------------------------------------------------------------------------------------------一月份李四和张三分别取报销了如下金额时间 编号 名字 报销金额
20120103 001 李四 200
20120104 001 李四 100
20120108 002 张三 300
20120109 002 张三 500
-------------------------------------------------------------------------------------------------------当导入下一月份的奖励时,假如导入2月份奖励表月份 编号 名字 奖励1 奖励2 奖励3 总共
201202 001 李四 1300 1200 1000 3500
201202 002 张三 1000 1500 2000 4500自动将下表更新,更新方式为:导入2月份奖励时,将1月份的未报销的余额加到2月份的奖励费用里,生成2月的可报数月份 编号 名字 可报数 已报数 余额
201201 001 李四 3000 300 2700
201201 002 张三 1500 800 700
201202 001 李四 6200
201202 002 张三 5200
--------------------------------------------------------------------------------------------------------请教各位大哥
小弟这样的思路要如何做?还有问题就是:如何在导入时,,自动将上月的余额跟本月的奖励费用加起来,当做本月的可报数. 先谢谢了
--> 测试数据:[tbl]
go
if object_id('[tbl]') is not null
drop table [tbl]
go
create table [tbl](
[name] varchar(1),
[date] varchar(5),
[num] int
)
go
insert [tbl]
select 'a','1-1号',1 union all
select 'b','1-2号',4 union all
select 'a','1-3号',8 union all
select 'a','1-4号',5 union all
select 'b','1-5号',6 union all
select 'b','1-6号',9;with t
as(
select ROW_NUMBER()over(partition by name
order by [date]) as id,
*,num as total from tbl
),
m as(
select id,name,[date],num,total from t where id=1
union all
select a.id,a.name,a.[date],a.num,b.total+a.num from t a
inner join m b on a.id=b.id+1 and a.name=b.name
)
select name,[date],num,total from m order by name/*
name date num total
a 1-3号 8 8
a 1-4号 5 13
a 1-1号 1 14
b 1-2号 4 4
b 1-5号 6 10
b 1-6号 9 19
*/--参考