-- 无分组
with 'create_account' as start_event,
    'login' as retuen_visit,
    `#account_id` as visit,
    toDate(addHours(`#event_time`, 8)) as date

select reg_date,
       cnt1,
       if(dateDiff('day', reg_date, toDate(now())) >= 2, toString(round(cnt2 * 100 / cnt1, 2)), '-') as `2留`,
       if(dateDiff('day', reg_date, toDate(now())) >= 3, toString(round(cnt3 * 100 / cnt1, 2)), '-') as `3留`,
       if(dateDiff('day', reg_date, toDate(now())) >= 4, toString(round(cnt4 * 100 / cnt1, 2)), '-') as `4留`,
       if(dateDiff('day', reg_date, toDate(now())) >= 5, toString(round(cnt5 * 100 / cnt1, 2)), '-') as `5留`,
       if(dateDiff('day', reg_date, toDate(now())) >= 6, toString(round(cnt6 * 100 / cnt1, 2)), '-') as `6留`,
       if(dateDiff('day', reg_date, toDate(now())) >= 7, toString(round(cnt7 * 100 / cnt1, 2)), '-') as `7留`

       from(select date, uniqExact(visit) as cnt1 from zhengba.event
where `#event_name` = start_event
group by date) reg left join
(select a.reg_date,
       sum(if(dateDiff('day',a.reg_date,b.visit_date)=1,1,0)) as cnt2,
       sum(if(dateDiff('day',a.reg_date,b.visit_date)=2,1,0)) as cnt3,
       sum(if(dateDiff('day',a.reg_date,b.visit_date)=3,1,0)) as cnt4,
       sum(if(dateDiff('day',a.reg_date,b.visit_date)=4,1,0)) as cnt5,
       sum(if(dateDiff('day',a.reg_date,b.visit_date)=5,1,0)) as cnt6,
       sum(if(dateDiff('day',a.reg_date,b.visit_date)=6,1,0)) as cnt7
from (select date as reg_date, visit from zhengba.event where `#event_name` = start_event group by reg_date, visit) a
         left join (select date as visit_date, visit from zhengba.event where `#event_name` = retuen_visit group by visit_date, visit) b on
a.visit = b.visit
group by a.reg_date) log on reg.date=log.reg_date
