简体   繁体   中英

Is there a way to to return one variable each from two separate tables, but only when two variables from each table match?

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?

That of course depends what you want to achieve

but you can start from.

This would join both CTE's on the same day and wallet, and list also the "values"

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
  )
SELECT bo."Day",bo."Wallet" ,bo."dexSHARE Redeemed",dexSHARE Sold"
FROM bonds_table bo JOIN sales_table sa ON sa."Wallet2  = o."Wallet"  AND sa."Day" = bo."Day"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM