【问题标题】:SQL Server 2014 LAG Function Running Start and End BalancesSQL Server 2014 LAG 函数运行开始和结束余额
【发布时间】:2016-09-09 15:09:03
【问题描述】:

希望有人可以帮助解决我正在尝试编写的查询。我正在尝试使用 SQL 2014 中的 LAG 函数编写一个运行总计/开始和结束余额查询。不幸的是,我似乎无法获得正确的语法,我想知道这是否可能。基本上,上一行的期末余额(endbal)值需要是下一行的期初余额(begbal),以此类推。期末余额计算为(期初余额 - principalcashflow)下面是我的示例查询和表变量设置:

declare @calccashflow table(
    recordid int,
    cashflowdate date,
    begbal float,
    endbal float,
    principalcashflow float,
    interestcashflow float
);

/*populate with test data */
insert into @calccashflow(recordid,cashflowdate,begbal,principalcashflow,interestcashflow)
values(1,'2016-09-01',100000000.00,100000.0,200.0);
insert into @calccashflow(recordid,cashflowdate,principalcashflow,interestcashflow)
values(2,'2016-10-01',200000.0,200.0);
insert into @calccashflow(recordid,cashflowdate,principalcashflow,interestcashflow)
values(3,'2016-11-01',300000.0,200.0);
insert into @calccashflow(recordid,cashflowdate,principalcashflow,interestcashflow)
values(4,'2016-12-01',300000.0,200.0);

/*set the ending balance of the first row to begbal-principalcashflow*/
update @calccashflow set endbal=begbal -principalcashflow where recordid=1;
SELECT  recordid, 
        cashflowdate ,
        begbal=case when recordid=1 then begbal 
                else lag(endbal,1,0) over (order by recordid) end,
        endbal=(case when recordid=1 then endbal 
                 else (case when recordid=1 then begbal 
                            else lag(endbal,1,0) over (order by recordid)    end) - principalcashflow end),
        principalcashflow,
        interestcashflow     
FROM @calccashflow

结果并不完全符合我的要求,在第二行之后,一切都崩溃了(见下图): Query Results

我想看的如下:

rowid  cashflowdate     begbal   endbal   principalcashflow
-----  ------------     ------   ------   -----------------
    1  2016-09-01     100000000 99900000              10000
    2  2016-10-01      99900000 99700000              20000
    3  2016-11-01      99700000 99400000              30000
    4  2016-12-01      99400000 99100000              30000

这里,上一个记录的期末余额成为下一个记录的期初余额。如上所述,每行的期末余额计算为 (begbal-principalcashflow)。

任何帮助将不胜感激!

【问题讨论】:

  • 样本数据和所需结果(以表格的形式)确实有助于传达您想要做的事情。
  • 请参阅上面提供的其他示例。

标签: sql sql-server sql-server-2014


【解决方案1】:

这是一种方法,使用累积和和first_value 窗口函数。请注意,您甚至不需要为第一条记录更新 endbal 列。我删除了更新。

declare @calccashflow table(
    recordid int,
    cashflowdate date,
    begbal float,
    endbal float,
    principalcashflow float,
    interestcashflow float
);

/*populate with test data */
insert into @calccashflow(recordid,cashflowdate,begbal,principalcashflow,interestcashflow)
values(1,'2016-09-01',100000000.00,100000.0,200.0);
insert into @calccashflow(recordid,cashflowdate,principalcashflow,interestcashflow)
values(2,'2016-10-01',200000.0,200.0);
insert into @calccashflow(recordid,cashflowdate,principalcashflow,interestcashflow)
values(3,'2016-11-01',300000.0,200.0);
insert into @calccashflow(recordid,cashflowdate,principalcashflow,interestcashflow)
values(4,'2016-12-01',300000.0,200.0);

with cte as (
  select recordid,
         cashflowdate,
         begbal,
         endbal = first_value(begbal) over (order by recordid) 
                  - sum(principalcashflow) over (order by recordid),
         principalcashflow,
         interestcashflow
  from @calccashflow
)
select recordid,
       cashflowdate,
       begbal = coalesce(begbal, endbal + principalcashflow),
       endbal,
       principalcashflow,
       interestcashflow
  from cte
 order by recordid;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 2013-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多