Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dividing two SQL queries

I am not super familiar with SQL queries, but I am trying to divide the results of two queries, ex. # of users with at least $x / total # of users, and I keep running into an error. Here is the code:

SELECT x.number / y.number
FROM 
(
    SELECT COUNT(address) 
    FROM transferAmounts 
    WHERE poolholdings > 0
) x
join
(
    SELECT COUNT(address)
    FROM transferAmounts
) y on 1=1

I tried to do it without the y as well, and it gave me an error still. Here is the entire code above the statement I am having trouble with:

WITH transfers AS (
    SELECT
    evt_tx_hash AS tx_hash,
    tr."from" AS address,
    -tr.value AS amount,
    contract_address
     FROM erc20."ERC20_evt_Transfer" tr
     WHERE contract_address =  '\xD533a949740bb3306d119CC777fa900bA034cd52'
UNION ALL
    SELECT
    evt_tx_hash AS tx_hash,
    tr."to" AS address,
    tr.value AS amount,
      contract_address
     FROM erc20."ERC20_evt_Transfer" tr 
     where contract_address = '\xD533a949740bb3306d119CC777fa900bA034cd52'
),
transferAmounts AS (
    SELECT address,
    
    sum(amount)/1e18 as poolholdings FROM transfers 
    
    GROUP BY 1
    ORDER BY 2 DESC
)

SELECT x.number / y.number
FROM 
(
SELECT COUNT(address) 
FROM transferAmounts 
WHERE poolholdings > 0
) x
join
(
SELECT COUNT(address)
FROM transferAmounts
) y on 1=1

I know the code from the first line up until the SELECT x.number / y.number is correct because I previously used it to count the # of people with $>0 in their crypto wallet. Now I want to modify it to divide that value by the total # of wallets.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you use aliases that where not defined

SELECT x._number / y._number
FROM 
(
    SELECT COUNT(address) _number
    FROM transferAmounts 
    WHERE poolholdings > 0
) x
join
(
    SELECT COUNT(address) _number
    FROM transferAmounts
) y on 1=1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading