I am working with a database and am using the following query:
SELECT
evt_block_time,
COUNT(*) filter (
WHERE
uniswap_version = 'v1'
) OVER (
ORDER BY
evt_block_time
) as v1_pairs,
COUNT(*) filter (
WHERE
uniswap_version = 'v2'
) OVER (
ORDER BY
evt_block_time
) as v2_pairs
FROM
(
SELECT
'v2' as uniswap_version,
evt_block_time
FROM
uniswap_v2."Factory_evt_PairCreated"
UNION ALL
SELECT
'v1' as uniswap_version,
evt_block_time
FROM
uniswap."Factory_evt_NewExchange"
ORDER BY
evt_block_time
) as creations
Here's a glimpse at what it returns:
I would like to do a few things. First of all, truncate the timestamps, evt_block_time, by week and then group by week.
- NOTE: I tried using date_trunc('week', evt_block_time) under each of my select statements, but it throws an error. See below:
SELECT
date_trunc('week', evt_block_time),
COUNT(*) filter (
WHERE
uniswap_version = 'v1'
) OVER (
ORDER BY
evt_block_time
) as v1_pairs,
COUNT(*) filter (
WHERE
uniswap_version = 'v2'
) OVER (
ORDER BY
evt_block_time
) as v2_pairs
FROM
(
SELECT
'v2' as uniswap_version,
date_trunc('week', evt_block_time)
FROM
uniswap_v2."Factory_evt_PairCreated"
UNION ALL
SELECT
'v1' as uniswap_version,
date_trunc('week', evt_block_time)
FROM
uniswap."Factory_evt_NewExchange"
ORDER BY
evt_block_time
) as creations
which returns:
Column "evt_block_time" does not exist at line 31, position 26.
Additionally, though I guess it's not required, I would like to only query data from the last 52 weeks (1 year).
Obviously, I'm kinda new to this SQL thing but I'm trying my best. Any help whatsoever would be appreciated!