Query editor
WITH base AS (SELECTdate,transaction_digest,sender AS owner,CAST(JSON_EXTRACT_SCALAR(event_json, '$.amount') AS BIGINT) AS raw_amount,JSON_EXTRACT_SCALAR(event_json, '$.lock_id') AS lock_id,JSON_EXTRACT_SCALAR(event_json, '$.permanent') AS permanent_indicator,-- Convert start and end dates from Unix timestamps to datetimeFROM_UNIXTIME(CAST(JSON_EXTRACT_SCALAR(event_json, '$.start') AS BIGINT)) AS start_date,CASEWHEN CAST(JSON_EXTRACT_SCALAR(event_json, '$.start') AS BIGINT) = 0THEN FROM_UNIXTIME(0)ELSE FROM_UNIXTIME(CAST(JSON_EXTRACT_SCALAR(event_json, '$.end') AS BIGINT))END AS end_date,-- Calculate lock duration based on permanent indicatorCASEWHEN JSON_EXTRACT_SCALAR(event_json, '$.permanent') = 'true' THEN 126144000ELSE CAST(JSON_EXTRACT_SCALAR(event_json, '$.end') AS BIGINT) - CAST(JSON_EXTRACT_SCALAR(event_json, '$.start') AS BIGINT)END AS lock_duration_seconds,-- Human-readable durationCASEWHEN JSON_EXTRACT_SCALAR(event_json, '$.permanent') = 'true' THEN'4 years (max)'ELSECONCAT(CAST(FLOOR((CAST(JSON_EXTRACT_SCALAR(event_json, '$.end') AS BIGINT) - CAST(JSON_EXTRACT_SCALAR(event_json, '$.start') AS BIGINT)) / 86400) AS VARCHAR),' days')END AS lock_duration_readableFROM sui.eventsWHERE event_type IN ('0xd5c34bb087893fec8faf905a1259c008776aaaae84e2d2d46deb1a6b9f95e358::voting_escrow::EventCreateLock','0xe616397e503278d406e184d2258bcbe7a263d0192cc0848de2b54b518165f832::voting_escrow::EventCreateLock'))SELECTAVG(lock_duration_seconds) AS avg_lock_duration_seconds,AVG(lock_duration_seconds) / 86400 AS avg_lock_duration_days,AVG(lock_duration_seconds) / 86400 / 30.44 AS avg_lock_duration_months,AVG(lock_duration_seconds) / 86400 / 365 AS avg_lock_duration_years,MIN(lock_duration_seconds) / 86400 AS min_lock_duration_days,MAX(lock_duration_seconds) / 86400 AS max_lock_duration_days,COUNT_IF(permanent_indicator = 'true') AS permanent_locks_count,COUNT_IF(permanent_indicator != 'true') AS non_permanent_locks_countFROM base