To display the token symbols alongside their addresses in your query's output, you'll need to join your result set with a table that maps token_address to token_symbol. Typically, this information is stored in a tokens table or a similar lookup table within your database.
Assuming you have a tokens table with at least the following columns:
token_address: The unique address of the token.
token_symbol: The human-readable symbol of the token (e.g., ETH, USDT).
Here's how you can modify your existing query to include the token_symbol in the output:
WITH wallet_list(wallet_address) AS (
VALUES
('0xef113e09cbe9f990e20a488779146d94c9b221ee'),
('0x448166a91e7bc50d0ac720c2fbed29e0963f5af8')
),
date_range AS (
SELECT
TRY_CAST('2024-09-10 00:00:00' AS TIMESTAMP) AS start_date,
TRY_CAST('2024-09-23 23:59:59' AS TIMESTAMP) AS end_date
),
tokens_traded AS (
SELECT DISTINCT
w.wallet_address,
t.token_bought_address AS token_address
FROM dex.trades AS t
INNER JOIN wallet_list AS w
ON t.tx_from = w.wallet_address
CROSS JOIN date_range AS dr
WHERE
t.block_time BETWEEN dr.start_date AND dr.end_date
UNION
SELECT DISTINCT
w.wallet_address,
t.token_sold_address AS token_address
FROM dex.trades AS t
INNER JOIN wallet_list AS w
ON t.tx_to = w.wallet_address
CROSS JOIN date_range AS dr
WHERE
t.block_time BETWEEN dr.start_date AND dr.end_date
),
intersected_tokens AS (
SELECT
token_address
FROM tokens_traded
GROUP BY
token_address
HAVING
COUNT(DISTINCT wallet_address) = (
SELECT COUNT(*) FROM wallet_list
)
)
SELECT
it.token_address,
tok.token_symbol
FROM intersected_tokens AS it
INNER JOIN tokens AS tok
ON it.token_address = tok.token_address
ORDER BY
it.token_address;
Explanation of Changes:
-
Join with the Tokens Table:
INNER JOIN tokens AS tok ON it.token_address = tok.token_address: This line joins the intersected_tokens CTE with the tokens table based on the token_address. This allows you to retrieve the corresponding token_symbol for each token_address.
-
Select Token Symbol:
tok.token_symbol: Added to the SELECT clause to include the human-readable symbol of the token in the final output.
-
Ensuring Correct Address Formatting:
- Quotation Marks: Make sure that the wallet addresses in the
VALUES clause are enclosed in single quotes to be recognized as strings. For example, use '0xef113e09cbe9f990e20a488779146d94c9b221ee' instead of 0xef113e09cbe9f990e20a488779146d94c9b221ee.
Additional Considerations:
-
Table and Column Names: Ensure that the tokens table and its columns (token_address and token_symbol) exist in your database schema. If they have different names, adjust the query accordingly.
-
Case Sensitivity: Token addresses are usually case-insensitive, but ensure that the casing matches or use functions to normalize them if necessary.
-
Performance: If the tokens table is large, consider indexing the token_address column to improve join performance.
Sample Output:
Assuming the join is successful and the tokens table contains the relevant symbols, your output will look something like this:
| token_address | token_symbol |
|---|
| 0x6B175474E89094C44Da98b954EedeAC495271d0F | DAI |
| 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48 | USDC |
| 0xC02aaA39b223FE8D0A0E5C4F27eAD9083C756Cc2 | WETH |
| ... | ... |
This modification provides a more user-friendly and informative result by displaying both the token addresses and their corresponding symbols.