Query editor
WITH top_protocols AS (SELECT projectFROM lending.borrowWHERE block_month >= DATE_TRUNC('month', CURRENT_DATE - INTERVAL '12' MONTH)AND blockchain IN ('ethereum','arbitrum','optimism','base','polygon','scroll','zksync','linea')AND transaction_type = 'borrow'GROUP BY projectORDER BY SUM(COALESCE(amount_usd, 0)) DESCLIMIT 5)SELECTDATE_TRUNC('month', b.block_time) AS month,b.project,SUM(CASE WHEN b.transaction_type = 'borrow' THEN COALESCE(b.amount_usd, 0) ELSE 0 END) AS borrow_volume_usd,ABS(SUM(CASE WHEN b.liquidator IS NOT NULL THEN COALESCE(b.amount_usd, 0) ELSE 0 END)) AS liquidation_volume_usd,COUNT(CASE WHEN b.liquidator IS NOT NULL THEN 1 END) AS liquidation_count,COUNT(DISTINCT CASE WHEN b.transaction_type = 'borrow' THEN b.borrower END) AS unique_borrowersFROM lending.borrow bJOIN top_protocols t ON b.project = t.projectWHERE b.block_month >= DATE_TRUNC('month', CURRENT_DATE - INTERVAL '12' MONTH)AND b.blockchain IN ('ethereum','arbitrum','optimism','base','polygon','scroll','zksync','linea')GROUP BY 1, 2ORDER BY 1 ASC, borrow_volume_usd DESC