Skip to content

Instantly share code, notes, and snippets.

@aknirmal90
Created January 10, 2019 10:32
Show Gist options
  • Save aknirmal90/c6885e18ba6362521107716609a14d96 to your computer and use it in GitHub Desktop.
Save aknirmal90/c6885e18ba6362521107716609a14d96 to your computer and use it in GitHub Desktop.
Balance Query for Google BQ public bitcoin dataset
WITH double_entry_book AS (
-- debits
SELECT
array_to_string(inputs.addresses, ",") as address
, inputs.type
, -inputs.value as value
FROM `crypto-etl-bitcoin-prod.bitcoin_blockchain.inputs` as inputs
UNION ALL
-- credits
SELECT
array_to_string(outputs.addresses, ",") as address
, outputs.type
, outputs.value as value
FROM `crypto-etl-bitcoin-prod.bitcoin_blockchain.outputs` as outputs
)
SELECT
address
, type
, sum(value) as balance
FROM double_entry_book
GROUP BY 1,2
ORDER BY balance DESC
LIMIT 1000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment