如何在其运行总计达到零的帐户上应用状态
Posted
技术标签:
【中文标题】如何在其运行总计达到零的帐户上应用状态【英文标题】:How to apply status on a account that has its running total reaching zero 【发布时间】:2019-12-14 20:18:18 【问题描述】:以下是我正在尝试操作的示例数据。
+----------------+------------------+---------+------+--------------+-------------+--+--+
| ACCOUNT_NUMBER | TRANSACTION_DATE | bal | Row# | RunningTotal | status | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 31/03/2015 | 82.61 | 4 | 82.61 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 31/03/2015 | 85.25 | 5 | 167.86 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 31/03/2015 | 93.61 | 6 | 261.47 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 30/04/2015 | 78.95 | 7 | 340.42 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 22/05/2015 | -62.04 | 8 | 278.38 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 31/05/2015 | 98.95 | 9 | 377.33 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 30/06/2015 | 79.5 | 10 | 456.83 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 15/07/2015 | -345.76 | 11 | 111.07 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 12/05/2016 | -111.07 | 12 | 0 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 31/03/2015 | 2.5 | 13 | 2.5 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 31/03/2015 | 2.5 | 14 | 5 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 31/03/2015 | 2.5 | 15 | 7.5 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 30/04/2015 | 2.5 | 16 | 10 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 31/05/2015 | 2.5 | 17 | 12.5 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 30/06/2015 | 0.67 | 18 | 13.17 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 30/07/2015 | -0.81 | 19 | 12.36 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 31/05/2018 | 5.08 | 20 | 17.44 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 30/11/2018 | 1.02 | 21 | 18.46 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 31/05/2019 | 1.48 | 22 | 19.94 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 31/03/2015 | 8.38 | 23 | 8.38 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 31/03/2015 | 10.65 | 24 | 19.03 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 31/03/2015 | 25.07 | 25 | 44.1 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 30/04/2015 | 12.21 | 26 | 56.31 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 30/04/2015 | -20 | 27 | 36.31 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 20/05/2015 | -36.31 | 28 | 0 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 31/05/2015 | -3.69 | 29 | -3.69 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 31/05/2015 | 13.17 | 30 | 9.48 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 30/06/2015 | 9 | 31 | 18.48 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 25/07/2015 | -18.48 | 32 | 0 | CLEARED
以下是用于应用发票状态的脚本。本质上,我希望能够确定某个帐户是否已经取消了所有发票。我有两个条件:
-
如果余额的 SUM 等于 0,则应用 CLEARED
第二个选项是我想要弄清楚的。如果以某种方式运行总和的最终总和不为零,但在它清除的地方让所有上述发票都清除标记。
select *,
(CASE WHEN sum(bal) OVER (PARTITION BY ACCOUNT_NUMBER ) = 0 THEN 'CLEARED'
WHEN sum(bal) OVER (PARTITION BY ACCOUNT_NUMBER order by Row#,TRANSACTION_DATE ) = 0 THEN 'CLEARED'
else 'NOT_CLEARED'
end) as status
from #running_totals
order by Row#, TRANSACTION_DATE
可以帮助我如何应用它 预期结果
+----------------+------------------+---------+------+--------------+-------------+--+--+
| ACCOUNT_NUMBER | TRANSACTION_DATE | bal | Row# | RunningTotal | status | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 31/03/2015 | 82.61 | 4 | 82.61 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 31/03/2015 | 85.25 | 5 | 167.86 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 31/03/2015 | 93.61 | 6 | 261.47 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 30/04/2015 | 78.95 | 7 | 340.42 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 22/05/2015 | -62.04 | 8 | 278.38 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 31/05/2015 | 98.95 | 9 | 377.33 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 30/06/2015 | 79.5 | 10 | 456.83 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 15/07/2015 | -345.76 | 11 | 111.07 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 12/05/2016 | -111.07 | 12 | 0 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 31/03/2015 | 2.5 | 13 | 2.5 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 31/03/2015 | 2.5 | 14 | 5 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 31/03/2015 | 2.5 | 15 | 7.5 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 30/04/2015 | 2.5 | 16 | 10 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 31/05/2015 | 2.5 | 17 | 12.5 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 30/06/2015 | 0.67 | 18 | 13.17 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 30/07/2015 | -0.81 | 19 | 12.36 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 31/05/2018 | 5.08 | 20 | 17.44 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 30/11/2018 | 1.02 | 21 | 18.46 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 31/05/2019 | 1.48 | 22 | 19.94 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 31/03/2015 | 8.38 | 23 | 8.38 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 31/03/2015 | 10.65 | 24 | 19.03 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 31/03/2015 | 25.07 | 25 | 44.1 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 30/04/2015 | 12.21 | 26 | 56.31 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 30/04/2015 | -20 | 27 | 36.31 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 20/05/2015 | -36.31 | 28 | 0 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 31/05/2015 | -3.69 | 29 | -3.69 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 31/05/2015 | 13.17 | 30 | 9.48 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 30/06/2015 | 9 | 31 | 18.48 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 25/07/2015 | -18.48 | 32 | 0 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| | | | | | | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| | | | | | | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| | | | | | | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
【问题讨论】:
“如果以某种方式运行总和的最终总和不为零,但在它清除的地方让所有上述发票都清除标记。”我不关注你想在那里说什么。预期结果和进一步解释将帮助我们为您提供帮助。 您的样本数据中的 DT_Amount 在哪里? 嗨 @mkRabbani 编辑到正确的 bal,如表中所示 感谢您的预期结果,但您没有进一步解释。 嗨@Larnu 可以说 961 它的运行总数达到零,但在其清算发票的底部有更多未清算的发票。所有高于零的发票基本上都应该被清除。 【参考方案1】:试试这个。鉴于您提供的数据表,我假设您已经拥有 RowNum 行。您或许可以改进这一点,但这是一个开始。
;WITH CTE AS(
select t1.ACCOUNT_NUMBER AN3, t1.RowNum RN3
from #temp t1
CROSS APPLY(select ACCOUNT_NUMBER AN2,RowNum RN2
from #temp where RunningTotal=0) t2 where t1.ACCOUNT_NUMBER = t2.AN2 and t1.RowNum <= t2.RN2
)
select ACCOUNT_NUMBER,TRANSACTION_DATE,bal,RowNum,RunningTotal,Status,
CASE WHEN t2.AN3 IS NOT NULL THEN 'CLEARED'
ELSE 'NOT CLEARED' END Status
from #temp t1
LEFT JOIN CTE t2 on t1.ACCOUNT_NUMBER = t2.AN3 and t1.RowNum = t2.RN3
【讨论】:
嗨@JM_,谢谢你成功了。只需在 CTE 之后的 select 上添加 distinct 。只返回唯一值。 @omah94 如果这提供了解决方案,我希望您将其标记为已回答。【参考方案2】:尝试以下逻辑-
SELECT *,
CASE
WHEN (SELECT MIN(ACCOUNT_NUMBER) FROM your_table) = ACCOUNT_NUMBER THEN 'CLEARED'
-- I have considered the MIN ACC_NUMBER as per your data
-- But you can also use a fix ACC_NUMBER if required like
-- WHEN 199= ACCOUNT_NUMBER THEN 'CLEARED'
WHEN
SUM(bal) OVER(
PARTITION BY ACCOUNT_NUMBER ORDER BY ACCOUNT_NUMBER
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) = 0 THEN 'CLEARED'
ELSE 'NOT_CLEARED'
END Status
FROM your_table
【讨论】:
哪里不匹配? 例如,我期待第一个帐号表示一切都已清除。由于帐户持有人已付清所有发票【参考方案3】:我认为您只需将状态清除且余额为 0 的最后一行的行号与余额为 0 的最后一行的行号进行比较即可:
select rt.*,
(case when sum(bal) over (partition by account_number) = 0
then 'CLEARED'
when max(case when status = 'CLEARED' and bal = 0 then Row# end) =
max(case when bal = 0 then Row# end)
then 'CLEARED'
else 'NOT_CLEARED'
end) as status
from #running_totals rt
order by Row#, TRANSACTION_DATE
【讨论】:
以上是关于如何在其运行总计达到零的帐户上应用状态的主要内容,如果未能解决你的问题,请参考以下文章
在 HDIinsight 集群上运行 spark 作业时如何解决此致命错误?会话 681 意外地达到了“死亡”的最终状态。查看日志: