OVER (PARTITION BY shop_id ORDER BY created_at DESC) AS ended_at, name, value FROM shopify.features WHERE name = 'storefront' ), point_of_sale_data AS ( SELECT shop_id, created_at AS started_at, FIRST_VALUE(created_at) OVER (PARTITION BY shop_id ORDER BY created_at) AS first_created_at, LAG(created_at) OVER (PARTITION BY shop_id ORDER BY created_at DESC) AS ended_at, name, value FROM shopify.features WHERE name = 'pos_login' ), timeline AS ( SELECT sf.shop_id, sf.started_at AS store_started_at, sf.ended_at AS store_ended_at, pos.first_created_at AS pos_first_started_at, pos.started_at AS pos_started_at, pos.ended_at AS pos_ended_at, sf.value=1 AS storefront, pos.value=1 AS pos, CONDITIONAL_CHANGE_EVENT(sf.value) OVER (PARTITION BY sf.shop_id ORDER BY sf.started_at) AS i1, CONDITIONAL_CHANGE_EVENT(pos.value) OVER (PARTITION BY pos.shop_id ORDER BY pos.started_at) AS i2 FROM storefront_data sf LEFT JOIN point_of_sale_data pos ON sf.shop_id = pos.shop_id AND sf.name='storefront' AND pos.name='pos_login' WHERE WITHIN_PERIOD(pos.started_at, sf.started_at, sf.ended_at) OR pos.started_at IS NULL ), unified_timeline AS ( SELECT DISTINCT t.shop_id, rt.started_at, rt.ended_at, t.pos_first_started_at, CASE WHEN rt.source = 'sign-up' THEN 'new signup' ELSE rt.source END AS source, t.storefront, t.pos FROM retail.trials_inclusive rt LEFT JOIN timeline t ON t.shop_id = rt.shop_id AND t.pos_started_at = rt.started_at WHERE rt.ended_at <> rt.started_at OR rt.ended_at IS NULL ), daily_pivot AS ( SELECT dd."Date", t.source, t.shop_id, t.pos_first_started_at, t.pos, t.storefront, t.pos AND t.storefront AS pos_and_web, COALESCE(s.retail_price,0) > 0 AND c.shop_id IS NOT NULL AS pos_customer, COALESCE(s.online_price,0) > 0 AND c.shop_id IS NOT NULL AS web_customer, COALESCE(s.retail_price,0) > 0 AND COALESCE(s.online_price,0) > 0 AND c.shop_id IS NOT NULL AS web_and_pos_customer FROM spark.date_dimension dd LEFT JOIN unified_timeline t ON WITHIN_PERIOD(dd."Date",t.started_at,COALESCE(t.ended_at,CURRENT_TIMESTAMP)) LEFT JOIN shopify.subscriptions s ON WITHIN_PERIOD(dd."Date",s.started_at,COALESCE(s.ended_at,CURRENT_TIMESTAMP)) AND s.shop_id = t.shop_id LEFT JOIN funnel.customers c ON c.shop_id = s.shop_id AND WITHIN_PERIOD(dd."Date", c.started_at, COALESCE(c.ended_at,CURRENT_TIMESTAMP)) AND c.active WHERE WITHIN_PERIOD(dd."Date",'2014-03-24=',CURRENT_TIMESTAMP) AND NOT s.plan_name IN ('frozen','cancelled') ), sources AS ( SELECT DISTINCT source FROM unified_timeline ) SELECT dd."#{params.date_interval}" AS date, ss.source, -- Hash shop_ids with their daily boolean values and count those COUNT( DISTINCT CASE WHEN dp.pos AND NOT dp.storefront THEN dp.shop_id ELSE NULL END ) AS pos_only_users, COUNT( DISTINCT CASE WHEN dp.pos_and_web THEN dp.shop_id ELSE NULL END ) AS pos_and_web_users, COUNT( DISTINCT CASE WHEN dp.pos_customer AND NOT dp.web_customer THEN dp.shop_id ELSE NULL END ) AS pos_only_customers, COUNT( DISTINCT CASE WHEN dp.web_and_pos_customer THEN dp.shop_id ELSE NULL END ) AS pos_and_web_customers FROM spark.date_dimension dd CROSS JOIN sources ss LEFT JOIN daily_pivot dp ON dp."Date" = dd."Date" AND dp.source = ss.source WHERE WITHIN_PERIOD(dd."Date",'2014-03-20',CURRENT_TIMESTAMP) GROUP BY 1,2 ORDER BY 1,2 5 pt text over there actual SELECT It is so big that it requires being 5 pt text to fit on this slide!