gpt4 book ai didi

SQL:在 OVER() 中使用 WHERE 子句?

转载 作者:行者123 更新时间:2023-12-03 23:55:20 25 4
gpt4 key购买 nike

我如何使用 WHEREOVER 中过滤的子句条款?

即从以下数据

LoanID | Principal | Tenor | AmortizingPrincipal 
----------------------------------------
1 20000 1 5000
1 20000 2 5000
1 20000 3 5000
1 20000 4 5000

我需要在每个 Tenor 中有一个带有 Balance Principal 的第四个虚拟列,如下所示:
LoanID | Principal | Tenor | AmortizingPrincipal | BalancePrinicpal 
-----------------------------------------------------------
1 20000 1 5000 20000
1 20000 2 5000 15000
1 20000 3 5000 10000
1 20000 4 5000 5000

像这样的东西:
SELECT 
BalancePrincipal = Principal - SUM(AmortizingPrincipal) OVER(PARTITION BY LoanID WHERE Tenor < this row's tenor)

更新:

以下查询为我提供了所需的结果:

选择
L1.*
,BalancePrincipal = AL1.Principal - ISNULL(Cumulative.AmortizingSum,0)

贷款L1
交叉申请
(
选择
AmortizingSum = SUM(AmortizingPrincipal)

贷款L2
在哪里
L1.LoanID = L2.LoanID

L1.男高音 > L2.男高音
) 累积

可以改善吗?

最佳答案

如果您使用的是 SQL Server 2012,您可能希望指定 ROWS/RANGE在您的 OVER :

Further limits the rows within the partition by specifying start and end points within the partition. This is done by specifying a range of rows with respect to the current row either by logical association or physical association. Physical association is achieved by using the ROWS clause.



其他数据库系统可能具有类似的功能。此功能是 SQL Server 2012 版中的新增功能。

关于SQL:在 OVER() 中使用 WHERE 子句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10027192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com