Query editor
WITH date_range AS ( -- Calculate the start and end date based on the user selection of either a specific date range or the last X days,it pioritizes the last x daysSELECTDATE_TRUNC('day', CASE WHEN '{{Last X Days}}' = '0' THEN TIMESTAMP '{{Start Date}}' ELSE NOW() - INTERVAL '{{Last X Days}}' day + INTERVAL '1' DAY END) AS start_date,DATE_TRUNC('day', CASE WHEN '{{Last X Days}}' = '0' THEN TIMESTAMP '{{End Date}}' ELSE NOW() END) AS end_date),top_deals_names AS (--Retrieve the names of the top deals based on the user selection (e.g., Top 3 deals)SELECTnameFROM"query_3978505(top_x_deals='{{Top X Deals}}')"),top_deal_contracts AS ( -- Get the contract addresses for the top dealsSELECTtd.name,contract_addressFROMtop_deals_names tdINNER JOINdune.polygonlabs.dataset_deal_sample ds ON ds.name = td.name),filtered_deals AS ( --Filter the deals by the provided (name, region, vertical)SELECTname,contract_addressFROMdune.polygonlabs.dataset_deal_sampleWHEREname = '{{Name}}' and region = '{{Global Region}}' AND vertical = '{{Vertical}}'),merged_deals AS ( --Merge top deals and filtered deals into a single list, handling cases where Top X Deals is not selectedSELECTdp.name,dp.contract_address,CASE WHEN {{Top X Deals}} = 0 THEN ds.name ELSE gs.name END AS selected_dealFROMdune.polygonlabs.dataset_deal_sample dpLEFT JOINfiltered_deals ds ON dp.name = ds.name AND dp.contract_address = ds.contract_addressLEFT JOINtop_deal_contracts gs ON dp.name = gs.name AND dp.contract_address = gs.contract_address),final_deals AS (--Final selection of deals based on the merged listSELECT*FROMmerged_dealsWHEREselected_deal IS NOT NULL),deal_transactions AS ( -- Aggregate transactions for the selected deals within the specified date rangeSELECTDATE_TRUNC('{{Time Period}}', block_date) AS period,COUNT(distinct "from") AS active_wallets,COUNT(*) as transactions