最后成功付款日期
Last Successful Payment Date
我如何加入以下两个 table 并从交易 Table 中仅获取 LastSucessfull 付款日期?我只想提取 LastSucesfullPaymentdates,这也应该考虑通过 returns;
LastSucesfull 付款日期的业务规则:
如果近期付款显示为Return或退款,它将显示在借记金额中,交易类型为'Return'或'Refund'。由于 return 和 Creditamount 因为我们试图收集金额,所以它将有两个条目与 Debitamount 具有相同的 Date One。那么这个场景应该考虑之前的成功支付日期。
如果在同一天没有任何 return 成功,它将显示在 Creditamount 中,交易类型为 'Payment'。这将是最后一次成功付款日期
如果交易类型是结算 - 这将是最后一次成功付款日期
目前,这是我用于上述输出的查询:
下面是交易table
Reference Number
PaymentNumber
TransactionType
Date
DebitAmount
CreditAMount
10484
1
Return
06/01/2022
242.61
10484
2
Payment
06/01/2022
242.61
10484
3
Payment
06/12/2021
242.61
10484
4
Payment
08/11/2021
242.61
10484
5
Payment
06/11/2021
242.61
10559
1
Payment
13/01/2022
0
529.65
10559
2
Return
10/01/2022
529.65
10559
3
Payment
10/01/2022
529.65
10559
4
Payment
10/12/2021
529.65
10598
1
Refund
29/12/2020
121.31
10598
2
Payment
11/12/2020
121.31
37473
1
Payment
22/01/2022
0
116.08
37473
2
Payment
22/12/2021
116.08
37473
3
Payment
22/11/2021
116.08
37466
1
Settlment
28/01/2022
1300
37466
2
Payment
28/12/2021
127.00
37466
3
Payment
28/11/2021
127.00
37466
4
Payment
28/10/2021
127.00
SELECT
ft.applicationid as 'Reference Number',
ROW_NUMBER() OVER (PARTITION BY ft.applicationid ORDER BY ft.valueDate DESC) AS PaymentNumber,
ft.[TransactionType],
CAST(Valuedate AS DATE) as 'Date',
ft.debitamount AS DebitAmount,
ft.creditamount AS CreditAMount
FROM dbo.FinancialTransaction22 as ft
WHERE ft.[TransactionType] in ('Payment','Return', 'Settlement', 'Refund') and ft.[Status]='cleared
以下全部参考Table
Reference Number
Customer
Status
Amount
10484
Glen
Active
12000
10559
Nyame
Active
5000
10598
Philip
Complete
6000
37473
Natalie
Active
6000
37466
Charlotte
Active
20000
目前我参考的是这个查询table:
Select Reference Number, Customer, Status, Amount from Reference table
我正在寻找具有 LastSucessfullPayment 列的新 table
我是 SQL 的初学者。但是,我正在尝试实现以下输出,并且我已根据我在事务 Table.
中使用的上述 3 条业务规则手动添加了 'Last Successful Payment Date' 日期
我想要的输出如下
Reference Number
Customer
Status
Amount
LastSucessfullPaymetDatet
10484
Glen
Active
12000
06/12/2021
10559
Nyame
Active
5000
13/01/2022
10598
Philip
Complete
6000
11/12/2021
37473
Natalie
Active
6000
22/01/2022
37466
Charlotte
Active
20000
28/01/2022
感谢支持。
基本上查询是使用 APPLY()
运算符为每个 Reference Number
.
获取 1 个事务
对于业务规则 1,这是通过检查以下表达式是否大于 0 来处理的
SUM(ISNULL(creditamount, 0) - ISNULL(debitamount, 0)) OVER (PARTITION BY Valuedate)
对于业务规则 2 和 3,CASE WHEN TransactionType = 'Settlement'
将为 Settlement
交易提供较低的 ROW_NUMBER()
价值。剩下的交易是ORDER BY valueDate DESC
ROW_NUMBER() OVER (ORDER BY CASE WHEN TransactionType = 'Settlement' THEN 1 ELSE 2 END,
ft.valueDate DESC)
查询:
SELECT r.[Reference Number], r.Customer, r.Status, r.Amount, d.LastSucessfullPaymetDatet
FROM Reference r
CROSS APPLY
(
SELECT TOP 1 LastSucessfullPaymetDatet = Valuedate
FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY CASE WHEN TransactionType = 'Settlement'
THEN 1
ELSE 2
END,
ft.valueDate DESC) AS PaymentNumber,
ft.Valuedate,
SUM(ISNULL(ft.creditamount, 0) - ISNULL(ft.debitamount, 0))
OVER (PARTITION BY ft.Valuedate) AS NettValueByDate
FROM FinancialTransaction22 ft
WHERE ft.applicationid = r.[Reference Number]
AND ft.[TransactionType] in ('Payment','Return', 'Settlement', 'Refund')
AND ft.[Status] = 'cleared'
) d
WHERE NettValueByDate> 0
ORDER BY PaymentNumber
) d
我如何加入以下两个 table 并从交易 Table 中仅获取 LastSucessfull 付款日期?我只想提取 LastSucesfullPaymentdates,这也应该考虑通过 returns;
LastSucesfull 付款日期的业务规则:
如果近期付款显示为Return或退款,它将显示在借记金额中,交易类型为'Return'或'Refund'。由于 return 和 Creditamount 因为我们试图收集金额,所以它将有两个条目与 Debitamount 具有相同的 Date One。那么这个场景应该考虑之前的成功支付日期。
如果在同一天没有任何 return 成功,它将显示在 Creditamount 中,交易类型为 'Payment'。这将是最后一次成功付款日期
如果交易类型是结算 - 这将是最后一次成功付款日期
目前,这是我用于上述输出的查询:
下面是交易table
Reference Number | PaymentNumber | TransactionType | Date | DebitAmount | CreditAMount |
---|---|---|---|---|---|
10484 | 1 | Return | 06/01/2022 | 242.61 | |
10484 | 2 | Payment | 06/01/2022 | 242.61 | |
10484 | 3 | Payment | 06/12/2021 | 242.61 | |
10484 | 4 | Payment | 08/11/2021 | 242.61 | |
10484 | 5 | Payment | 06/11/2021 | 242.61 | |
10559 | 1 | Payment | 13/01/2022 | 0 | 529.65 |
10559 | 2 | Return | 10/01/2022 | 529.65 | |
10559 | 3 | Payment | 10/01/2022 | 529.65 | |
10559 | 4 | Payment | 10/12/2021 | 529.65 | |
10598 | 1 | Refund | 29/12/2020 | 121.31 | |
10598 | 2 | Payment | 11/12/2020 | 121.31 | |
37473 | 1 | Payment | 22/01/2022 | 0 | 116.08 |
37473 | 2 | Payment | 22/12/2021 | 116.08 | |
37473 | 3 | Payment | 22/11/2021 | 116.08 | |
37466 | 1 | Settlment | 28/01/2022 | 1300 | |
37466 | 2 | Payment | 28/12/2021 | 127.00 | |
37466 | 3 | Payment | 28/11/2021 | 127.00 | |
37466 | 4 | Payment | 28/10/2021 | 127.00 |
SELECT
ft.applicationid as 'Reference Number',
ROW_NUMBER() OVER (PARTITION BY ft.applicationid ORDER BY ft.valueDate DESC) AS PaymentNumber,
ft.[TransactionType],
CAST(Valuedate AS DATE) as 'Date',
ft.debitamount AS DebitAmount,
ft.creditamount AS CreditAMount
FROM dbo.FinancialTransaction22 as ft
WHERE ft.[TransactionType] in ('Payment','Return', 'Settlement', 'Refund') and ft.[Status]='cleared
以下全部参考Table
Reference Number | Customer | Status | Amount |
---|---|---|---|
10484 | Glen | Active | 12000 |
10559 | Nyame | Active | 5000 |
10598 | Philip | Complete | 6000 |
37473 | Natalie | Active | 6000 |
37466 | Charlotte | Active | 20000 |
目前我参考的是这个查询table:
Select Reference Number, Customer, Status, Amount from Reference table
我正在寻找具有 LastSucessfullPayment 列的新 table 我是 SQL 的初学者。但是,我正在尝试实现以下输出,并且我已根据我在事务 Table.
中使用的上述 3 条业务规则手动添加了 'Last Successful Payment Date' 日期我想要的输出如下
Reference Number | Customer | Status | Amount | LastSucessfullPaymetDatet |
---|---|---|---|---|
10484 | Glen | Active | 12000 | 06/12/2021 |
10559 | Nyame | Active | 5000 | 13/01/2022 |
10598 | Philip | Complete | 6000 | 11/12/2021 |
37473 | Natalie | Active | 6000 | 22/01/2022 |
37466 | Charlotte | Active | 20000 | 28/01/2022 |
感谢支持。
基本上查询是使用 APPLY()
运算符为每个 Reference Number
.
对于业务规则 1,这是通过检查以下表达式是否大于 0 来处理的
SUM(ISNULL(creditamount, 0) - ISNULL(debitamount, 0)) OVER (PARTITION BY Valuedate)
对于业务规则 2 和 3,CASE WHEN TransactionType = 'Settlement'
将为 Settlement
交易提供较低的 ROW_NUMBER()
价值。剩下的交易是ORDER BY valueDate DESC
ROW_NUMBER() OVER (ORDER BY CASE WHEN TransactionType = 'Settlement' THEN 1 ELSE 2 END,
ft.valueDate DESC)
查询:
SELECT r.[Reference Number], r.Customer, r.Status, r.Amount, d.LastSucessfullPaymetDatet
FROM Reference r
CROSS APPLY
(
SELECT TOP 1 LastSucessfullPaymetDatet = Valuedate
FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY CASE WHEN TransactionType = 'Settlement'
THEN 1
ELSE 2
END,
ft.valueDate DESC) AS PaymentNumber,
ft.Valuedate,
SUM(ISNULL(ft.creditamount, 0) - ISNULL(ft.debitamount, 0))
OVER (PARTITION BY ft.Valuedate) AS NettValueByDate
FROM FinancialTransaction22 ft
WHERE ft.applicationid = r.[Reference Number]
AND ft.[TransactionType] in ('Payment','Return', 'Settlement', 'Refund')
AND ft.[Status] = 'cleared'
) d
WHERE NettValueByDate> 0
ORDER BY PaymentNumber
) d