WITH swap_data AS (
SELECT
CAST(time AS DATE) AS activity_date,
COUNT(*) AS swap_count,
SUM(inputamount + outputamount) AS swap_volume
FROM @portals.transactions.swaps
WHERE inputtoken LIKE 'ethereum:%'
AND outputtoken LIKE 'ethereum:%'
GROUP BY activity_date
),
deposit_data AS (
SELECT
CAST(time AS DATE) AS activity_date,
COUNT(*) AS deposit_count,
SUM(amount) AS deposit_volume
FROM @portals.transactions.deposits
WHERE key LIKE 'ethereum:%'
GROUP BY activity_date
),
withdraw_data AS (
SELECT
CAST(time AS DATE) AS activity_date,
COUNT(*) AS withdrawal_count,
SUM(amount) AS withdrawal_volume
FROM @portals.transactions.withdraws
WHERE key LIKE 'ethereum:%'
GROUP BY activity_date
)
SELECT
COALESCE(s.activity_date, d.activity_date, w.activity_date) AS date,
s.swap_count,
s.swap_volume,
d.deposit_count,
d.deposit_volume,
w.withdrawal_count,
w.withdrawal_volume
FROM swap_data s
FULL OUTER JOIN deposit_data d ON s.activity_date = d.activity_date
FULL OUTER JOIN withdraw_data w ON COALESCE(s.activity_date, d.activity_date) = w.activity_date
ORDER BY date;