问题描述:有以下一个SQLSERVER表记录员工调休在未来12个月每个月的剩余情况,出现负数的月份,需要优先用负数前面的月份抵扣掉负数,不够抵扣的再用负数后面的月份抵扣,没有发生抵扣的月份保持原值。
-- 创建测试表并插入示例数据
CREATE TABLE #EmployeeLeave (
人员ID INT,
月份 INT,
本月过期加班 DECIMAL(10,2),
次月过期加班 DECIMAL(10,2),
第3月过期加班 DECIMAL(10,2),
第4月过期加班 DECIMAL(10,2),
第5月过期加班 DECIMAL(10,2),
第6月过期加班 DECIMAL(10,2),
第7月过期加班 DECIMAL(10,2),
第8月过期加班 DECIMAL(10,2),
第9月过期加班 DECIMAL(10,2),
第10月过期加班 DECIMAL(10,2),
第11月过期加班 DECIMAL(10,2),
第12月过期加班 DECIMAL(10,2)
);
INSERT INTO #EmployeeLeave VALUES
(1, 202510, 4, 0, 0, 0, 0, 8.5, 0, 0, 0, 0, -7.5, 0);
想要的效果:
第6月过期加班=5.0,其他月份为0