View difference between Paste ID: pbFLtff3 and 1n9r2PY0
SHOW: | | - or go back to the newest paste.
1
select purchase_month, utm_source, hear_about_us, sum("Purchases") as "Purchases" 
2
--, sum("Net Sales") as "Net Sales", sum("GMV") as "GMV", sum(consultations) as "Consultations"
3
from 
4
	( 			
5
		select utm_source, hear_about_us,
6
			case 
7
				when tv_user = true then 'TV'
8
				when utm_source is null and hear_about_us is null then 'Organic (Unknown)' 
9
				when utm_source is null and hear_about_us is not null then hear_about_us || ' (User provided)'
10
				when utm_source in ('Google') and utm_campaign like '%Branded%' and google_branded_search is not null then google_branded_search
11
				when utm_source in ('Google') and utm_campaign like '%Branded%' then 'Google: Branded'
12
				when utm_source in ('Google') then 'Google: Non-branded'
13
				else utm_source end as "Source",
14
			purchase_month,
15
			purchase_month_num,
16
			sum(net_sales) as "Net Sales",
17
			sum(gmv) as "GMV",
18
			count(case when refunded in (false) then user_id end) as "Purchases"
19
		from (
20
			select a.id as user_id, 
21
			coalesce(f.source, g.utm_source) as utm_source,
22
			coalesce(f.medium, g.utm_medium) as utm_medium,
23
			google_branded_search,
24
			f.campaign as utm_campaign, b.hear_about_us,
25
			to_char(start_date, 'Month') as purchase_month,
26
			date_part('month'::text, start_date) AS purchase_month_num,
27
			case when t.user_id is not null then true end as tv_user,
28
			refunded,
29
			(total_amount_in_cents - amount_refunded) / 100 as net_sales,
30
			(subtotal_amount_in_cents + shipping_amount_in_cents - amount_refunded) / 100 as  gmv
31
			from users a
32
			inner join profiles b on a.id = b.user_id
33
			inner join (
34
				select row_number() over (partition by user_id order by created_at) as order_number, user_id,
35
				created_at at time zone 'UTC' as start_date, order_type,
36
				case when total_amount_in_cents in (amount_refunded) then true else false end as refunded,
37
				total_amount_in_cents, subtotal_amount_in_cents, shipping_amount_in_cents, amount_refunded
38
				from orders
39
				where order_type like ('plan%')
40
			) c on b.user_id = c.user_id and order_number = 1
41
			left join (
42
				select row_number () over (partition by user_id order by updated_at desc) as i, *
43
				from utm_params	
44
				where (source not in ('customer.io') and campaign not in ('FB-lead-ad'))
45
			) f on f.i = 1 and a.id = f.user_id
46
			left join scratch.channel_to_utm_mappings g on b.hear_about_us = g.hear_about_us
47
			left join scratch_tv_users_all_5 t on a.id = t.user_id
48
			left join consultations cons on cons.user_id = a.id
49
			where a.email not like '%@thirtymadison%' 
50
			and a.email not like '%@keeps%'
51
			and start_date >= '2018-01-16'
52
		) a
53
		group by "Source", purchase_month, purchase_month_num, utm_source, hear_about_us
54
		order by "Purchases" desc
55
) a left join scratch.marketing_source_bucketing b on lower(a."Source") = lower(b.source)
56
where source_bucket = 'Other - Paid'
57
group by 
58
--source_bucket, 
59
purchase_month, purchase_month_num, utm_source, hear_about_us
60
order by purchase_month_num, sum("Purchases") desc;