This is my ethereum_transaction_receipts
table in redshift:
CREATE TABLE "crypto_blockchains"."ethereum_transaction_receipts" (
"hash" character varying(256),
"block_number" bigint,
"tx_time" timestamp without time zone,
"event_generator_address" character varying(256),
"event_hash" character varying(256),
"event_decoded" character varying(256),
"event_param_1" character varying(256),
"event_param_2" character varying(256),
"data" text,
"token_from" character varying(256),
"token_to" character varying(256),
"token_amount" numeric(38,5)
)
tx_time
is the transaction time, token_from
is from address, token_to
is to address and token_amount
is the transaction amount. I want the balance snapshot for each user for each day.
For example: If there is one transaction from A -> B on 2017-01-01 and another on A -> B on 2018-04-09, I want entries for each date between min and max date for the balances of A and B.
I have tried this query to get sum of token amount
for token from
user, same way I can calculate the sum of token amount
for token to
user and join them and subtract to get output, but this query gives me one row per transaction and also I don't get details for the user who have stopped making transactions after a certain date.
select
date(tx_time) as date,
token_from as addr,
sum(nullif(token_amount,null)) over (partition by event_generator_address, token_from order by tx_time rows unbounded preceding) as amt
from crypto_blockchains.ethereum_transaction_receipts
where event_decoded = 'Transfer'
Is there a better way to calculate balances per day using window functions.
Edit 1: Sample Data:
token to,token from,token amount,tx_time
A,B,0.4,1/1/17
B,A,0.4,1/3/17
Desired Result:
user,balance,date
A,0,1/1/17
A,0.4,1/2/17
A,0,1/3/17
B,0,1/1/17
B,-0.4,1/2/17
B,0,1/3/17