Monthly Cohort

                Never    
SQL
       
with t4 as (select 
t3.year,
t3.month,
concat(cast(t3.year as string),'-',cast(t3.month as string)) as year_month,
 --t3.first_category_slug,
 --t3.month_0_order,
 --t3.month_0_multi_category,
sum(month_0)/sum(month_0) as month_0,
sum(month_1)/sum(month_0) as month_1,
sum(month_2)/sum(month_0) as month_2,
sum(month_3)/sum(month_0) as month_3,
sum(month_4)/sum(month_0) as month_4,
sum(month_5)/sum(month_0) as month_5,
sum(month_6)/sum(month_0) as month_6,
sum(month_7)/sum(month_0) as month_7,
sum(month_8)/sum(month_0) as month_8,
sum(month_9)/sum(month_0) as month_9,
sum(month_10)/sum(month_0) as month_10,
sum(month_11)/sum(month_0) as month_11,
sum(month_12)/sum(month_0) as month_12,
sum(month_13)/sum(month_0) as month_13,
sum(month_14)/sum(month_0) as month_14,
sum(month_15)/sum(month_0) as month_15,
sum(month_16)/sum(month_0) as month_16,
sum(month_17)/sum(month_0) as month_17,
sum(month_18)/sum(month_0) as month_18,
sum(month_19)/sum(month_0) as month_19,
sum(month_20)/sum(month_0) as month_20,
sum(month_21)/sum(month_0) as month_21,
sum(month_22)/sum(month_0) as month_22,
sum(month_23)/sum(month_0) as month_23,
sum(month_24)/sum(month_0) as month_24,
sum(month_25)/sum(month_0) as month_25,
sum(month_26)/sum(month_0) as month_26,
sum(month_27)/sum(month_0) as month_27,
sum(month_28)/sum(month_0) as month_28,
sum(month_29)/sum(month_0) as month_29,
sum(month_30)/sum(month_0) as month_30,
sum(month_31)/sum(month_0) as month_31,
sum(month_32)/sum(month_0) as month_32,
sum(month_33)/sum(month_0) as month_33,
sum(month_34)/sum(month_0) as month_34,
sum(month_35)/sum(month_0) as month_35,
sum(month_36)/sum(month_0) as month_36,
sum(month_37)/sum(month_0) as month_37,
sum(month_38)/sum(month_0) as month_38,
sum(month_39)/sum(month_0) as month_39,
sum(month_40)/sum(month_0) as month_40,
sum(month_41)/sum(month_0) as month_41,
sum(month_42)/sum(month_0) as month_42,
sum(month_43)/sum(month_0) as month_43,
sum(month_44)/sum(month_0) as month_44,
sum(month_45)/sum(month_0) as month_45,
sum(month_46)/sum(month_0) as month_46,
sum(month_47)/sum(month_0) as month_47,
sum(month_48)/sum(month_0) as month_48,
sum(month_49)/sum(month_0) as month_49,
sum(month_50)/sum(month_0) as month_50,
sum(month_51)/sum(month_0) as month_51,
sum(month_52)/sum(month_0) as month_52,
sum(month_53)/sum(month_0) as month_53,
sum(month_54)/sum(month_0) as month_54,
sum(month_55)/sum(month_0) as month_55,
sum(month_56)/sum(month_0) as month_56,
sum(month_57)/sum(month_0) as month_57,
sum(month_58)/sum(month_0) as month_58,
sum(month_59)/sum(month_0) as month_59,
sum(month_60)/sum(month_0) as month_60,
sum(month_61)/sum(month_0) as month_61,
sum(month_62)/sum(month_0) as month_62,
sum(month_63)/sum(month_0) as month_63,
sum(month_64)/sum(month_0) as month_64,
sum(month_65)/sum(month_0) as month_65,
sum(month_66)/sum(month_0) as month_66,
sum(month_67)/sum(month_0) as month_67,
sum(month_68)/sum(month_0) as month_68,
sum(month_69)/sum(month_0) as month_69,
sum(month_70)/sum(month_0) as month_70,
sum(month_71)/sum(month_0) as month_71,
sum(month_72)/sum(month_0) as month_72,
sum(month_73)/sum(month_0) as month_73,
sum(month_74)/sum(month_0) as month_74,
sum(month_75)/sum(month_0) as month_75,
sum(month_76)/sum(month_0) as month_76,
sum(month_77)/sum(month_0) as month_77,
sum(month_78)/sum(month_0) as month_78,
sum(month_79)/sum(month_0) as month_79,
sum(month_80)/sum(month_0) as month_80,
sum(month_81)/sum(month_0) as month_81,
sum(month_82)/sum(month_0) as month_82,
sum(month_83)/sum(month_0) as month_83,
sum(month_84)/sum(month_0) as month_84,
sum(month_85)/sum(month_0) as month_85,
sum(month_86)/sum(month_0) as month_86,
sum(month_87)/sum(month_0) as month_87,
sum(month_88)/sum(month_0) as month_88,
sum(month_89)/sum(month_0) as month_89,
sum(month_90)/sum(month_0) as month_90


from
(with 
t1 as(
select 
*
from
(select 
buyer_id,
payed_at,
first_order_at,
rank() OVER (PARTITION BY buyer_id
                    ORDER BY payed_at DESC) AS rnk,
category_slug
 
from shared.orders
order by 1 desc ,2 DESC) as t
where rnk=1 )

select 
t2.buyer_id,
EXTRACT(year FROM t2.first_order_at) as year,
EXTRACT(month FROM t2.first_order_at) as month,
t1.category_slug as first_category_slug,
month_0 as month_0_order,
month_0_multi_category,
if(month_0>0 ,1,0) as month_0,
if(month_1>0 ,1,0) as month_1,
if(month_2>0 ,1,0) as month_2,
if(month_3>0 ,1,0) as month_3,
if(month_4>0 ,1,0) as month_4,
if(month_5>0 ,1,0) as month_5,
if(month_6>0 ,1,0) as month_6,
if(month_7>0 ,1,0) as month_7,
if(month_8>0 ,1,0) as month_8,
if(month_9>0 ,1,0) as month_9,
if(month_10>0 ,1,0) as month_10,
if(month_11>0 ,1,0) as month_11,
if(month_12>0 ,1,0) as month_12,
if(month_13>0 ,1,0) as month_13,
if(month_14>0 ,1,0) as month_14,
if(month_15>0 ,1,0) as month_15,
if(month_16>0 ,1,0) as month_16,
if(month_17>0 ,1,0) as month_17,
if(month_18>0 ,1,0) as month_18,
if(month_19>0 ,1,0) as month_19,
if(month_20>0 ,1,0) as month_20,
if(month_21>0 ,1,0) as month_21,
if(month_22>0 ,1,0) as month_22,
if(month_23>0 ,1,0) as month_23,
if(month_24>0 ,1,0) as month_24,
if(month_25>0 ,1,0) as month_25,
if(month_26>0 ,1,0) as month_26,
if(month_27>0 ,1,0) as month_27,
if(month_28>0 ,1,0) as month_28,
if(month_29>0 ,1,0) as month_29,
if(month_30>0 ,1,0) as month_30,
if(month_31>0 ,1,0) as month_31,
if(month_32>0 ,1,0) as month_32,
if(month_33>0 ,1,0) as month_33,
if(month_34>0 ,1,0) as month_34,
if(month_35>0 ,1,0) as month_35,
if(month_36>0 ,1,0) as month_36,
if(month_37>0 ,1,0) as month_37,
if(month_38>0 ,1,0) as month_38,
if(month_39>0 ,1,0) as month_39,
if(month_40>0 ,1,0) as month_40,
if(month_41>0 ,1,0) as month_41,
if(month_42>0 ,1,0) as month_42,
if(month_43>0 ,1,0) as month_43,
if(month_44>0 ,1,0) as month_44,
if(month_45>0 ,1,0) as month_45,
if(month_46>0 ,1,0) as month_46,
if(month_47>0 ,1,0) as month_47,
if(month_48>0 ,1,0) as month_48,
if(month_49>0 ,1,0) as month_49,
if(month_50>0 ,1,0) as month_50,
if(month_51>0 ,1,0) as month_51,
if(month_52>0 ,1,0) as month_52,
if(month_53>0 ,1,0) as month_53,
if(month_54>0 ,1,0) as month_54,
if(month_55>0 ,1,0) as month_55,
if(month_56>0 ,1,0) as month_56,
if(month_57>0 ,1,0) as month_57,
if(month_58>0 ,1,0) as month_58,
if(month_59>0 ,1,0) as month_59,
if(month_60>0 ,1,0) as month_60,
if(month_61>0 ,1,0) as month_61,
if(month_62>0 ,1,0) as month_62,
if(month_63>0 ,1,0) as month_63,
if(month_64>0 ,1,0) as month_64,
if(month_65>0 ,1,0) as month_65,
if(month_66>0 ,1,0) as month_66,
if(month_67>0 ,1,0) as month_67,
if(month_68>0 ,1,0) as month_68,
if(month_69>0 ,1,0) as month_69,
if(month_70>0 ,1,0) as month_70,
if(month_71>0 ,1,0) as month_71,
if(month_72>0 ,1,0) as month_72,
if(month_73>0 ,1,0) as month_73,
if(month_74>0 ,1,0) as month_74,
if(month_75>0 ,1,0) as month_75,
if(month_76>0 ,1,0) as month_76,
if(month_77>0 ,1,0) as month_77,
if(month_78>0 ,1,0) as month_78,
if(month_79>0 ,1,0) as month_79,
if(month_80>0 ,1,0) as month_80,
if(month_81>0 ,1,0) as month_81,
if(month_82>0 ,1,0) as month_82,
if(month_83>0 ,1,0) as month_83,
if(month_84>0 ,1,0) as month_84,
if(month_85>0 ,1,0) as month_85,
if(month_86>0 ,1,0) as month_86,
if(month_87>0 ,1,0) as month_87,
if(month_88>0 ,1,0) as month_88,
if(month_89>0 ,1,0) as month_89,
if(month_90>0 ,1,0) as month_90

from
(
select 
buyer_id,
first_order_at,
count(distinct case when date(payed_at) between date(first_order_at) and DATE_ADD(DATE (first_order_at),INTERVAL 29 DAY) then category_slug else null end) as month_0_multi_category,
count(distinct case when date(payed_at) between date(first_order_at) and DATE_ADD(DATE (first_order_at),INTERVAL 29 DAY) then order_id else null end) as month_0,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 30 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 59 DAY) then order_id else null end)  as month_1,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 60 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 89 DAY) then order_id else null end)  as month_2,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 90 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 119 DAY) then order_id else null end)  as month_3,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 120 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 149 DAY) then order_id else null end)  as month_4,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 150 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 179 DAY) then order_id else null end)  as month_5,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 180 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 209 DAY) then order_id else null end)  as month_6,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 210 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 239 DAY) then order_id else null end)  as month_7,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 240 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 269 DAY) then order_id else null end)  as month_8,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 270 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 299 DAY) then order_id else null end)  as month_9,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 300 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 329 DAY) then order_id else null end)  as month_10,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 330 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 359 DAY) then order_id else null end)  as month_11,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 360 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 389 DAY) then order_id else null end)  as month_12,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 390 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 419 DAY) then order_id else null end)  as month_13,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 420 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 449 DAY) then order_id else null end)  as month_14,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 450 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 479 DAY) then order_id else null end)  as month_15,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 480 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 509 DAY) then order_id else null end)  as month_16,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 510 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 539 DAY) then order_id else null end)  as month_17,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 540 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 569 DAY) then order_id else null end)  as month_18,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 570 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 599 DAY) then order_id else null end)  as month_19,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 600 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 629 DAY) then order_id else null end)  as month_20,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 630 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 659 DAY) then order_id else null end)  as month_21,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 660 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 689 DAY) then order_id else null end)  as month_22,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 690 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 719 DAY) then order_id else null end)  as month_23,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 720 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 749 DAY) then order_id else null end)  as month_24,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 750 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 779 DAY) then order_id else null end)  as month_25,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 780 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 809 DAY) then order_id else null end)  as month_26,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 810 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 839 DAY) then order_id else null end)  as month_27,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 840 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 869 DAY) then order_id else null end)  as month_28,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 870 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 899 DAY) then order_id else null end)  as month_29,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 900 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 929 DAY) then order_id else null end)  as month_30,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 930 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 959 DAY) then order_id else null end)  as month_31,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 960 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 989 DAY) then order_id else null end)  as month_32,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 990 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1019 DAY) then order_id else null end)  as month_33,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1020 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1049 DAY) then order_id else null end)  as month_34,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1050 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1079 DAY) then order_id else null end)  as month_35,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1080 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1109 DAY) then order_id else null end)  as month_36,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1110 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1139 DAY) then order_id else null end)  as month_37,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1140 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1169 DAY) then order_id else null end)  as month_38,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1170 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1199 DAY) then order_id else null end)  as month_39,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1200 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1229 DAY) then order_id else null end)  as month_40,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1230 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1259 DAY) then order_id else null end)  as month_41,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1260 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1289 DAY) then order_id else null end)  as month_42,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1290 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1319 DAY) then order_id else null end)  as month_43,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1320 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1349 DAY) then order_id else null end)  as month_44,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1350 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1379 DAY) then order_id else null end)  as month_45,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1380 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1409 DAY) then order_id else null end)  as month_46,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1410 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1439 DAY) then order_id else null end)  as month_47,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1440 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1469 DAY) then order_id else null end)  as month_48,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1470 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1499 DAY) then order_id else null end)  as month_49,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1500 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1529 DAY) then order_id else null end)  as month_50,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1530 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1559 DAY) then order_id else null end)  as month_51,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1560 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1589 DAY) then order_id else null end)  as month_52,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1590 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1619 DAY) then order_id else null end)  as month_53,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1620 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1649 DAY) then order_id else null end)  as month_54,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1650 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1679 DAY) then order_id else null end)  as month_55,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1680 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1709 DAY) then order_id else null end)  as month_56,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1710 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1739 DAY) then order_id else null end)  as month_57,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1740 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1769 DAY) then order_id else null end)  as month_58,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1770 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1799 DAY) then order_id else null end)  as month_59,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1800 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1829 DAY) then order_id else null end)  as month_60,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1830 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1859 DAY) then order_id else null end)  as month_61,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1860 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1889 DAY) then order_id else null end)  as month_62,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1890 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1919 DAY) then order_id else null end)  as month_63,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1920 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1949 DAY) then order_id else null end)  as month_64,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1950 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 1979 DAY) then order_id else null end)  as month_65,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 1980 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 2009 DAY) then order_id else null end)  as month_66,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 2010 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 2039 DAY) then order_id else null end)  as month_67,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 2040 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 2069 DAY) then order_id else null end)  as month_68,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 2070 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 2099 DAY) then order_id else null end)  as month_69,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 2100 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 2129 DAY) then order_id else null end)  as month_70,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 2130 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 2159 DAY) then order_id else null end)  as month_71,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 2160 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 2189 DAY) then order_id else null end)  as month_72,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 2190 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 2219 DAY) then order_id else null end)  as month_73,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 2220 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 2249 DAY) then order_id else null end)  as month_74,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 2250 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 2279 DAY) then order_id else null end)  as month_75,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 2280 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 2309 DAY) then order_id else null end)  as month_76,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 2310 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 2339 DAY) then order_id else null end)  as month_77,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 2340 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 2369 DAY) then order_id else null end)  as month_78,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 2370 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 2399 DAY) then order_id else null end)  as month_79,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 2400 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 2429 DAY) then order_id else null end)  as month_80,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 2430 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 2459 DAY) then order_id else null end)  as month_81,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 2460 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 2489 DAY) then order_id else null end)  as month_82,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 2490 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 2519 DAY) then order_id else null end)  as month_83,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 2520 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 2549 DAY) then order_id else null end)  as month_84,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 2550 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 2579 DAY) then order_id else null end)  as month_85,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 2580 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 2609 DAY) then order_id else null end)  as month_86,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 2610 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 2639 DAY) then order_id else null end)  as month_87,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 2640 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 2669 DAY) then order_id else null end)  as month_88,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 2670 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 2699 DAY) then order_id else null end)  as month_89,
count(distinct case when date(payed_at) between DATE_ADD(DATE (first_order_at), INTERVAL 2700 DAY) and DATE_ADD(DATE (first_order_at), INTERVAL 2729 DAY) then order_id else null end)  as month_90
from shared.orders
group by 1,2
order by 1 desc,2 desc )  as t2
left join t1 on t1.buyer_id=t2.buyer_id) as t3
group by 1,2,3
order by 1,2,3,4,5) 

select 
year_month,'month_0' as months,month_0 as Values from t4  union all 
select 
year_month,'month_1' as months,month_1 as Values from t4  union all
select 
year_month,'month_2' as months,month_2 as Values from t4  union all
select 
year_month,'month_3' as months,month_3 as Values from t4  union all
select 
year_month,'month_4' as months,month_4 as Values from t4  union all
select 
year_month,'month_5' as months,month_5 as Values from t4  union all
select 
year_month,'month_6' as months,month_6 as Values from t4  union all
select 
year_month,'month_7' as months,month_7 as Values from t4  union all
select 
year_month,'month_8' as months,month_8 as Values from t4  union all
select 
year_month,'month_9' as months,month_9 as Values from t4  union all
select 
year_month,'month_10' as months,month_10 as Values from t4  union all
select 
year_month,'month_11' as months,month_11 as Values from t4  union all
select 
year_month,'month_12' as months,month_12 as Values from t4  union all
select 
year_month,'month_13' as months,month_13 as Values from t4  union all
select 
year_month,'month_14' as months,month_14 as Values from t4  union all
select 
year_month,'month_15' as months,month_15 as Values from t4  union all
select 
year_month,'month_16' as months,month_16 as Values from t4  union all
select 
year_month,'month_17' as months,month_17 as Values from t4  union all
select 
year_month,'month_18' as months,month_18 as Values from t4  union all
select 
year_month,'month_19' as months,month_19 as Values from t4  union all
select 
year_month,'month_20' as months,month_20 as Values from t4  union all
select 
year_month,'month_21' as months,month_21 as Values from t4  union all
select 
year_month,'month_22' as months,month_22 as Values from t4  union all
select 
year_month,'month_23' as months,month_23 as Values from t4  union all
select 
year_month,'month_24' as months,month_24 as Values from t4  union all
select 
year_month,'month_25' as months,month_25 as Values from t4  union all
select 
year_month,'month_26' as months,month_26 as Values from t4  union all
select 
year_month,'month_27' as months,month_27 as Values from t4  union all
select 
year_month,'month_28' as months,month_28 as Values from t4  union all
select 
year_month,'month_29' as months,month_29 as Values from t4  union all
select 
year_month,'month_30' as months,month_30 as Values from t4  

order by 1,2,3

Raw Text