Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
sql用户保留计算_Sql - Fatal编程技术网

sql用户保留计算

sql用户保留计算,sql,Sql,我在雅典娜有这样一个表记录,一个用户一个月一行: month, id 2020-05 1 2020-05 2 2020-05 5 2020-06 1 2020-06 5 2020-06 6 需要计算百分比=(上个月和本月的用户数)/(上个月的总用户数)。 像上面的例子一样,5月和6月1日都有用户,5月总共有3个用户,这应该计算出2/3*100的百分比 with monthly_mau AS (SELECT month as mauMonth,

我在雅典娜有这样一个表
记录
,一个用户一个月一行:

month,   id
2020-05   1
2020-05   2
2020-05   5
2020-06   1
2020-06   5
2020-06   6

需要计算
百分比=(上个月和本月的用户数)/(上个月的总用户数)。
像上面的例子一样,5月和6月1日都有用户,5月总共有3个用户,这应该计算出2/3*100的百分比

with monthly_mau AS 
    (SELECT  month as mauMonth,
         date_format(date_add('month',1,cast(concat(month,'-01') AS date)), '%Y-%m') AS nextMonth,
         count(distinct userid) AS monthly_mau
    FROM records
    GROUP BY  month
    ORDER BY  month),
 
 retention_mau AS 
    (SELECT 
         month,
         count(distinct useridLeft) AS retention_mau
    FROM ( 
        (SELECT 
         userid as useridLeft,month as monthLeft,
         date_format(date_add('month',1,cast(concat(month,'-01') AS date)), '%Y-%m') AS nextMonth
        FROM records ) AS prior
        INNER JOIN 
            (SELECT 
         month ,
         userid 
            FROM records ) AS current
                ON 
                    prior.useridLeft = current.userid
                    AND prior.nextMonth = current.month )
     WHERE userid is not null
     GROUP BY   month
     ORDER BY   month )
 
SELECT *, cast(retention_mau AS double)/cast(monthly_mau AS double)*100 AS retention_mau_percentage 
FROM monthly_mau as m
    INNER JOIN monthly_retention_mau AS r
        ON m.nextMonth = r.month 
 order by  r.month     

这给了我100%的百分比,这是不对的。有什么想法吗?

嗯。假设每个用户每月有一行,则可以使用窗口函数和条件聚合:

select month, count(*) as num_users,
       sum(case when prev_month = dateadd('month', -1, month) then 1 else 0 end) as both_months
from (select r.*,
             cast(concat(month, '-01') AS date) as month_date,
             lag(cast(concat(month, '-01') AS date)) over (partition by id order by month) as prev_month_date
      from records r
     ) r
group by month;