我有逻辑来计算收据金额和收货日期的总和。我不确定下面的t-sql是否适用于我的逻辑。
我需要收据日期的最长时间,并且将用于任何收据日期,即。,01-01-2014这应该给我支付人员ID的预付金额。此外,如果最大收货日期值为负,那么我需要从计算中删除它。
WITH rec_cte (amt,recdate) AS
(
select -40, 01-04-2014 UNION ALL
select -40,01-04-2014 UNION ALL
select 40,20-04-2013 UNION ALL
select -20,10-04-2012 UNION ALL
select 50,20-04-2011 UNION ALL
select 40,20-04-2010 UNION ALL
)
SELECT SUM(amt),recdate from rec_cte
答案 0 :(得分:0)
至于我能够解释你的问题,你正在寻找基于ID的SUM(amt)。
WITH rec_cte AS
(
select 1 as id,-40 as amt, '01-04-2014' as recdate UNION ALL
select 1, -40,'01-04-2014' UNION ALL
select 2, 40,'20-04-2013' UNION ALL
select 3, -20,'10-04-2012' UNION ALL
select 2, 50,'20-04-2011' UNION ALL
select 1, 40,'20-04-2010'
)
SELECT id, SUM(amt) from rec_cte
--where recdate= '01-04-2014'
group by id
having SUM(amt)>=0