I have two separate queries that both work correctly. I have two common columns in each table "Day" and "Wallet". I have one table that returns (Day, Wallet, Withdrawals) and another that returns (Day, Wallet, Sales). I'd like to have a single query that returns joins "Withdrawals" and "Sales" but only for wallets that have a sale and withdrawal on the same day.
The code below allows m to call bonds_table (withdrawals) and sales_table, but I don't know how to return a single table that shows "dexSHARE Redeemed" and "dexSHARE Sold" when a single wallet makes both on the same day.
WITH
bonds_table AS(
SELECT
DATE_TRUNC('day', "call_block_time") AS "Day",
"_depositor" AS Wallet,
sum("output_0") / 1e18 AS "dexSHARE Redeemed"
FROM
dexfinance."wBNB_bond_for_dexshare_call_redeem"
WHERE
"call_success" IS TRUE --Verify transaction was successful
group by
1,
2
),
--This table is for sales of the tokens
sales_table AS(
SELECT
derivedTable1."Day",
derivedTable1."to" AS Wallet,
sum(derivedTable1."amountIn" / 1e18) AS "dexSHARE Sold"
FROM
-- This combines the sales from both tables so they can be summed
(
SELECT
DATE_TRUNC('day', "call_block_time") AS "Day",
"amountIn",
"to"
FROM
pancakeswap_v2."PancakeRouter_call_swapExactTokensForETH" -- This table is for tokens to wBNB
WHERE
"path" [1] = '\xf4914e6d97a75f014acfcf4072f11be5cffc4ca6' -- Sale of dexSHARE
AND call_success is TRUE -- Verify transaction was successful
AND "call_block_time" >= TO_TIMESTAMP('2022-05-24', 'yyyy-mm-dd')
UNION
SELECT
DATE_TRUNC('day', "call_block_time") AS "Day",
"amountIn",
"to" AS Wallet
FROM
pancakeswap_v2."PancakeRouter_call_swapExactTokensForTokens" -- This table is for tokens to other tokens (not wBNB)
WHERE
"path" [1] = '\xf4914e6d97a75f014acfcf4072f11be5cffc4ca6' -- Sale of dexSHARE
AND call_success is TRUE -- Verify transaction was successful
AND "call_block_time" >= TO_TIMESTAMP('2022-05-24', 'yyyy-mm-dd') -- Sales after this date (launch of Bond)
) AS derivedTable1
GROUP BY
1,
2
)
I know I need to append a SELECT statement at the end, but I'm lost with how to structure it. Can anyone help me head down the correct direction?