Sure! You can modify your SQL query to include an additional output table that shows all tokens traded by each wallet within the specified date range. To achieve this, you'll add another SELECT statement at the end of your query. This approach assumes that your SQL environment supports multiple result sets in a single query. If it doesn't, you might need to run separate queries or use a different method based on your specific database system.
Here's the updated SQL code with the additional output table:
WITH wallet_list(wallet_address) AS (
VALUES
('0xccF9E8b1eADb5a5799391E28Bb873B27D87B93c4'),
('0x9D251EFFBe8862eaDCe3077A0b7c0CA85D9757C7')
),
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 (
-- Tokens bought by wallets
SELECT DISTINCT
w.wallet_address,
LOWER(t.token_bought_address) AS token_address, -- Normalize address casing
t.token_bought_symbol AS token_symbol
FROM dex.trades AS t
INNER JOIN wallet_list AS w
ON LOWER(t.tx_from) = LOWER(w.wallet_address) -- Normalize address casing
CROSS JOIN date_range AS dr
WHERE
t.block_time BETWEEN dr.start_date AND dr.end_date
AND LOWER(t.token_bought_address) != '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2' -- Exclude WETH when bought
UNION
-- Tokens sold by wallets
SELECT DISTINCT
w.wallet_address,
LOWER(t.token_sold_address) AS token_address, -- Normalize address casing
t.token_sold_symbol AS token_symbol
FROM dex.trades AS t
INNER JOIN wallet_list AS w
ON LOWER(t.tx_to) = LOWER(w.wallet_address) -- Normalize address casing
CROSS JOIN date_range AS dr
WHERE
t.block_time BETWEEN dr.start_date AND dr.end_date
AND LOWER(t.token_sold_address) != '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2' -- Exclude WETH when sold
),
intersected_tokens AS (
SELECT
token_address,
MAX(token_symbol) AS token_symbol -- Assuming symbol is consistent per address
FROM tokens_traded
GROUP BY
token_address
HAVING
COUNT(DISTINCT wallet_address) = (
SELECT COUNT(*) FROM wallet_list
)
)
-- First Output: Intersected Tokens Traded by All Wallets
SELECT
it.token_address,
it.token_symbol
FROM intersected_tokens AS it
ORDER BY
it.token_address;
-- Second Output: All Tokens Traded by Each Wallet
SELECT
tt.wallet_address,
tt.token_address,
tt.token_symbol
FROM tokens_traded AS tt
ORDER BY
tt.wallet_address,
tt.token_address;
Explanation of the Changes:
-
Added LOWER Function for Normalization:
- To ensure that the wallet addresses are compared in a case-insensitive manner, the
LOWER function is applied to both the addresses in the wallet_list and the transaction fields (tx_from, tx_to, token_bought_address, token_sold_address).
-
Second SELECT Statement:
- After the first
SELECT statement that retrieves the intersected_tokens, a second SELECT statement is added.
- This second
SELECT retrieves all tokens traded by each wallet within the specified date range.
- It selects the
wallet_address, token_address, and token_symbol from the tokens_traded CTE.
- The results are ordered by
wallet_address and then by token_address for clarity.
-
Comments for Clarity:
- Added comments to differentiate between the two output sections.
Considerations:
-
Multiple Result Sets:
- Ensure that your SQL client or environment supports multiple result sets. Some interfaces may only display the results of the last
SELECT statement. If that's the case, you might need to run the queries separately or use a different approach based on your specific environment.
-
Performance:
- Depending on the size of your
dex.trades table and the number of wallets, the query might be resource-intensive. Ensure proper indexing on columns like tx_from, tx_to, token_bought_address, token_sold_address, and block_time to optimize performance.
-
Data Consistency:
- The query assumes that the token symbols are consistent for each token address. If there's a possibility of symbol changes or inconsistencies, you might need to handle those cases differently.
-
Date Range Flexibility:
- If you need to frequently change the date range, consider parameterizing the
date_range CTE or passing the dates as query parameters, depending on your database system.
Example Output:
- First Output (Intersected Tokens Traded by All Wallets):
| token_address | token_symbol |
|---|
| 0xabc123... | ABC |
| 0xdef456... | DEF |
| ... | ... |
- Second Output (All Tokens Traded by Each Wallet):
| wallet_address | token_address | token_symbol |
|---|
| 0xccF9E8b1eADb5a5799391E28Bb873B27D87B93c4 | 0xabc123... | ABC |
| 0xccF9E8b1eADb5a5799391E28Bb873B27D87B93c4 | 0xdef456... | DEF |
| 0x9D251EFFBe8862eaDCe3077A0b7c0CA85D9757C7 | 0xabc123... | ABC |
| 0x9D251EFFBe8862eaDCe3077A0b7c0CA85D9757C7 | 0xghi789... | GHI |
| ... | ... | ... |
This setup provides both the intersected tokens traded by all specified wallets and a detailed list of all tokens traded by each individual wallet within the specified date range.