You are on page 1of 2

WITH

t AS (
SELECT
1 AS n,
evt_block_time
FROM
liquity_ethereum.BorrowerOperations_evt_TroveCreated
UNION ALL
SELECT
-1 AS n,
evt_block_time
FROM
liquity_ethereum.BorrowerOperations_evt_TroveUpdated
WHERE
_coll = CAST(0 AS UINT256)
UNION ALL
SELECT
-1 AS n,
evt_block_time
FROM
liquity_ethereum.TroveManager_evt_TroveUpdated
WHERE
_coll = CAST(0 AS UINT256)
),
h AS (
SELECT
DATE_TRUNC('hour', evt_block_time) AS hour,
SUM(n) AS troveCountChange
FROM
t
GROUP BY
1
)
SELECT
hour,
SUM(troveCountChange) OVER (
ORDER BY
hour
) AS numberOfTroves
FROM
h
ORDER BY
1

/* v1 code
with t as (
select 1 as n, "evt_block_time"
from liquity."BorrowerOperations_evt_TroveCreated"

union all

select -1 as n, "evt_block_time"
from liquity."BorrowerOperations_evt_TroveUpdated"
where "_coll" = 0

union all

select -1 as n, "evt_block_time"
from liquity."TroveManager_evt_TroveUpdated"
where "_coll" = 0
),

h as (
select
DATE_TRUNC('hour', "evt_block_time") as "hour",
SUM(n) as "troveCountChange"
from t
group by 1
)

select
"hour",
SUM("troveCountChange") over (order by "hour") as "numberOfTroves"
from h
order by 1
;
*/

You might also like