[英]How to calculate the running balance of an asset based on INPUT and OUTPUT
I'm looking at different blockchain transactions and wanted to create a running balance of a given asset based on INPUT_ADDRESS
(the address sending the currency) INPUT_AMOUNT
(the amount being sent by an INPUT_ADDRESS), OUTPUT_ADDRESS
(the address receiving the currency) and OUTPUT_AMOUNT
(the amount being received by an OUTPUT_ADDRESS)我正在查看不同的区块链交易,并希望根据
INPUT_ADDRESS
(发送货币的地址) INPUT_AMOUNT
(由 INPUT_ADDRESS 发送的金额)、 OUTPUT_ADDRESS
(接收货币的地址)和OUTPUT_AMOUNT
创建给定资产的运行余额(OUTPUT_ADDRESS 收到的金额)
Here's a sample of a table I'm using:这是我正在使用的表格示例:
BLOCK_DATE | BLOCK_HEIGHT | TRANS_HASH | INPUT_ADDRESS | OUTPUT_ADDRESS | INPUT_AMOUNT | OUTPUT_AMOUNT
01/11/2020 190 15c7853 abc xyz1 -0.01 0.0001
01/11/2020 190 14v9876 abc xyz2 -0.50 0.70
01/11/2020 191 19vc842 abc xyz3 -5.03 0.413
01/12/2020 192 20ff4d3 abc xyz4 -0.06 0.201
01/12/2020 192 154gf34 xyz1 abc -0.07 0.18
01/12/2020 192 45f4ti5 ggg abc -0.10 0.24
01/12/2020 192 33cv5c5 jjj abc -0.08 1.13
If I were to calculate a running sum of address abc
, what's an efficient way of going about this?如果我要计算地址
abc
的运行总和,那么解决此问题的有效方法是什么? I tried using something like:我尝试使用类似的东西:
SELECT BLOCK_DATE, BLOCK_HEIGHT, TRANS_HASH, INPUT_ADDRESS, OUTPUT_ADDRESS, INPUT_AMOUNT, OUTPUT_AMOUNT, SUM (INPUT_AMOUNT) OVER (ORDER BY DATE) AS RunningAgeTotal
FROM TRANSACTION_TABLE
WHERE INPUT_ADDRESS = abc
In this particular example, the total balance for abc would be the sum of OUTPUT_AMOUNT where abc is the OUTPUT_ADDRESS (ie 0.18 + 0.24 + 1.13) + the sum of INPUT_AMOUNT where abc is the INPUT_ADDRESS (ie -0.01 + -0.50 + -5.03 + -0.06).在此特定示例中,abc 的总余额将是 OUTPUT_AMOUNT 的总和,其中 abc 是 OUTPUT_ADDRESS(即 0.18 + 0.24 + 1.13)+ INPUT_AMOUNT 的总和,其中 abc 是 INPUT_ADDRESS(即 -0.01 + -0.50 + -5.03 + -0.06)。 So, 1.55 + (-5.60) = -4.05
所以,1.55 + (-5.60) = -4.05
But I don't think this is the right way of going about this and I'm not sure how to account for the OUTPUT_AMOUNT (eg when abc receives is an OUTPUT_ADDRESS and receives an OUTPUT_AMOUNT)但我认为这不是解决这个问题的正确方法,我不确定如何解释 OUTPUT_AMOUNT(例如,当 abc 收到的是 OUTPUT_ADDRESS 并收到 OUTPUT_AMOUNT 时)
Is this what you want?这是你想要的吗?
select t.*,
sum(case when input_address = 'ABC' then input_amount
when output_address = 'ABC' then output_amount
end) over (order by block_date) as running_amount
from transaction_table t
where 'ABC' in (input_address, output_address);
This is a cumulative sum of the amounts aligned with the input/output columns.这是与输入/输出列对齐的金额的累积总和。
EDIT:编辑:
You may want:您可能想要:
sum(case when input_address = 'ABC' then input_amount
when output_address = 'ABC' then output_amount
end) over (order by block_date, block_height) as running_amount
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.