我有一个基于交易日期的派生临时表,如下所示:
-----------------------------------------------------------------------------------
|Account_No| Customer_Name | Invoice_No | Amount | Reason_Comment | Trans_Code |
-----------------------------------------------------------------------------------
|12345678 | John Doe | A23782998 | 326.28 | Payment | 2 |
-----------------------------------------------------------------------------------
|12345678 | John Doe | A23782998 | 173.72 | Adjustment | 3 |
-----------------------------------------------------------------------------------
|12345678 | John Doe | A23782998 | 0.00 | Paid In Full | X |
-----------------------------------------------------------------------------------
|23456789 | Bob Marley | B58787934 | 500.00 | Payment | 2 |
-----------------------------------------------------------------------------------
|23456789 | Bob Marley | B58787934 | 0.00 | Paid In Full | X |
-----------------------------------------------------------------------------------
|89034537 | Eric Clapton | C78236428 | 0.00 | Paid In Full | X |
-----------------------------------------------------------------------------------
正如您可能注意到的那样,Bob Markey付款并且他的帐户显示为“已付款” Eric Clapton很顺利。他提前支付了款项,因此已经显示为“全额支付” John Doe在这里需要一点'调整'的帮助,以便我们可以将他的账户余额归零,从而'全额支付'
付款交易的Trans_Code为2,调整3和全额付款为X.
我想排除在TransCode中只有“已付费”原因或X的交易。
我试过了:
SELECT *
FROM #temp_table
WHERE Account_No NOT IN
(SELECT Account_No FROM #temp_table
WHERE Reason_Comment = 'Paid In FUll'
AND Reason_Comment <> 'PAYMENT'
AND Reason_Comment <> 'ADJUSTMENT')
也许,我应该使用NOT EXISTS
修改
这是我正在使用的表格。由于我指定了日期范围,因此某些帐户只有一行,即“已付款”行,因为调整或付款发生在指定的日期范围之前。例如,下表中的最后一行(Johnson Doe)。我想排除最后一行。
----------------------------------------------------------------------------------------------------------
|Account_No| Customer_Name | Invoice_No | Amount | Trans_No |Reason_Comment | Trans_Date | Trans_Code|
----------------------------------------------------------------------------------------------------------
|12345678 | John Doe | A23782998 | 326.28 |1234567 |Payment | 2018-04-01 | 2|
----------------------------------------------------------------------------------------------------------
|12345678 | John Doe | A23782998 | 687.58 | |Amount Due | | 4|
----------------------------------------------------------------------------------------------------------
|23456789 | Jane Doe | B12378299 | 123.56 |2345678 |Payment | 2018-04-02 | 2|
----------------------------------------------------------------------------------------------------------
|23456789 | Jane Doe | B12378299 | 336.44 |3456789 |Adjustment | 2018-04-03 | 3|
----------------------------------------------------------------------------------------------------------
|23456789 | Jane Doe | B12378299 | | |Paid In Full | | X|
----------------------------------------------------------------------------------------------------------
|34567890 | Jonas Doe | C34567998 | 500.55 |4567891 |Payment | 2018-04-06 | 2|
----------------------------------------------------------------------------------------------------------
|34567890 | Jonas Doe | C34567998 | | |Paid In Full | | X|
----------------------------------------------------------------------------------------------------------
|45678912 | Johnson Doe | D45678756 | | |Paid In Full | | X|
----------------------------------------------------------------------------------------------------------
答案 0 :(得分:0)
您可以使用:
SELECT *
FROM tab t
WHERE NOT EXISTS (SELECT 1
FROM tab t2
WHERE t.Account_no = t2.Account_no
AND t.Invoice_no = t2.Invoice_no
AND (Reason_Comment IN ('Paid In FUll')
OR TransCode = 'X')
);
答案 1 :(得分:0)
- 您可以用不同的方式编写查询
ON DUPLICATE KEY UPDATE portfolioValue = VALUES(portfolioValue)
答案 2 :(得分:0)
正如我在评论中所述,您的帖子并不清楚您究竟需要排除什么,但从您提供的示例和查询中,我得出结论,您需要排除没有的客户任何调整或分期付款计划(人们喜欢你的例子中的Eric Clapton),因为你需要跟踪已经全额支付的付款(提前或早于到期付款),你不应该跟进分期付款或调整以确保您获得全额付款? 这就是我理解你的问题的方式,这对我来说是有意义的(也许)。
如果您正在寻找什么,那么您可以这样做:
更新(使用新提供的示例)
SELECT
Account_No,
Customer_Name,
Invoice_No,
Amount,
Trans_No,
Reason_Comment,
Trans_Date,
Trans_Code
FROM(
SELECT
Account_No,
Customer_Name,
Invoice_No,
Amount,
Trans_No,
Reason_Comment,
Trans_Code,
CASE
WHEN
COUNT(Account_No) OVER(PARTITION BY Account_No) = 1 AND Trans_Code = 'X'
THEN NULL
ELSE
COUNT(Account_No) OVER(PARTITION BY Account_No)
END AS NumberOfPayments,
Trans_Date
FROM Trans
) D
WHERE
NumberOfPayments IS NOT NULL
AND (
CAST(Trans_Date AS DATE) >= '2018-04-01'
AND CAST(Trans_Date AS DATE) <= '2018-04-06'
OR Trans_Date IS NULL
)
这将返回:
| Account_No | Customer_Name | Invoice_No | Amount | Trans_No | Reason_Comment | Trans_Date | Trans_Code |
|------------|---------------|------------|--------|----------|----------------|----------------------|------------|
| 12345678 | John Doe | A23782998 | 326.28 | 1234567 | Payment | 2018-04-01T00:00:00Z | 2 |
| 12345678 | John Doe | A23782998 | 687.58 | (null) | Amount Due | (null) | 4 |
| 23456789 | Jane Doe | B12378299 | 123.56 | 2345678 | Payment | 2018-04-02T00:00:00Z | 2 |
| 23456789 | Jane Doe | B12378299 | 336.44 | 3456789 | Adjustment | 2018-04-03T00:00:00Z | 3 |
| 23456789 | Jane Doe | B12378299 | (null) | (null) | Paid In Full | (null) | X |
| 34567890 | Jonas Doe | C34567998 | 500.55 | 4567891 | Payment | 2018-04-06T00:00:00Z | 2 |
| 34567890 | Jonas Doe | C34567998 | (null) | (null) | Paid In Full | (null) | X |
NumberOfPayments 将根据发票号计算每个帐号的付款。
您可以根据需要更改CASE
规则,还可以添加对分期付款有用的付款订单(付款1,付款2,付款3 ..等)。您只需添加以下行:
ROW_NUMBER() OVER(PARTITION BY Invoice_No ORDER BY Trans_Code) AS PaymnetNumber