我有两个独立的查询,它们都能正确工作。我在每个表中有两个常见的列"Day“和"Wallet”。我有一个返回表(日,钱包,提款)和另一个返回表(日,钱包,销售)。我希望有一个查询,返回联接“取款”和“销售”,但只对钱包有销售和提款在同一天。
下面的代码允许m调用bonds_table (取款)和sales_table,但我不知道如何返回一个表,该表显示“已赎回的dexSHARE”和“已售出的dexSHARE”。
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语句,但是我不知道如何构造它。有人能帮我朝正确的方向走吗?
发布于 2022-08-25 23:06:49
当然,这取决于你想要实现什么。
但你可以从。
这将在同一天加入CTE和钱包,并列出“值”
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"https://stackoverflow.com/questions/73494436
复制相似问题