QUERY CODE
'''
WITH ACTIVE_WALLETS AS(
SELECT
a.event_from AS ACTIVE,
date_trunc('DAY', a.block_timestamp) AS FECHA,
b.balance,
b.balance_type
FROM
terra.transfers a
INNER JOIN terra.daily_balances b
ON a.event_from = b.address AND b.currency = 'LUNA' AND b.balance > 0 AND b.date = CURRENT_DATE - 1
WHERE a.event_amount > 0 AND FECHA >= CURRENT_DATE - 90
),
SINGLE_ADDRESS_BALANCE AS(
SELECT
DISTINCT(ACTIVE),
BALANCE,
BALANCE_TYPE,
CASE
WHEN (BALANCE > 0 AND BALANCE <= 20) THEN '01-MOUSE'
WHEN (BALANCE > 20 AND BALANCE <= 200) THEN '02-CAT'
WHEN (BALANCE > 200 AND BALANCE <= 2000) THEN '03-DOG'
WHEN (BALANCE > 2000 AND BALANCE <= 20000) THEN '04-HORSE'
WHEN (BALANCE > 20000 AND BALANCE <= 200000) THEN '05-ELEPHANT'
WHEN (BALANCE > 200000) THEN '06-DINOSAUR'
END AS ANIMALS
FROM
ACTIVE_WALLETS
)
SELECT
ANIMALS,
BALANCE_TYPE,
COUNT(ANIMALS)
FROM
SINGLE_ADDRESS_BALANCE
GROUP BY ANIMALS, BALANCE_TYPE
ORDER BY ANIMALS