Fred红叶 2020-07-11 22:41 采纳率: 66.7%
浏览 141
已结题

查询新增用户产生 活跃行为 之后造成流失的用户数据

需求:查询3月1号-3月7号新增的用户,产生某活跃行为的总数,以及产生该行为后流失的用户数


流失定义:本周活跃,下周未活跃的用户定为流失


user表:

图片说明


user_action表:

图片说明


预期结果:


图片说明


指标含义:


用户总数:在3/1号-3/7号注册的用户产生对应的行为总用户数


流失用户数:发生对应行为后,之后的一周没有活跃记录的用户数

本人试写过一段查询逻辑,但是报错,大概知道错误和Case那块有关,但不知道具体如何解决

select
t1.行为,
count(DISTINCT t1.memberid) as 总用户数,
sum(case when t1.memberid not in (select t1.memberid from t1 where time = 1) then 1 else 0 end) as 流失用户数
from
    (
    SELECT 
        fua.UserID as memberid,
        week(fua.CreateTime) - week(t.created_at) as time,
        Action as 行为
        FROM
            (
                select user_id, created_at 
                from user
                where created_at between Date_format('2019-03-01 00:00:00','%Y-%m-%d') and Date_format('2019-03-07 23:59:59','%Y-%m-%d')
            ) as t
        INNER JOIN user_action fua 
        ON t.user_id = fua.UserID
     where Action is not NULL
    GROUP BY memberid,行为
    ) as t1
GROUP BY t1.行为


  • 写回答

1条回答 默认 最新

  • Json-Huang 2020-07-12 11:11
    关注

    子查询没有看到有聚合函数,GROUP BY memberid,行为 这行先去掉试试,即:

    select
    t1.行为,
    count(DISTINCT t1.memberid) as 总用户数,
    sum(case when t1.memberid not in (select t1.memberid from user as t1 where time = 1) then 1 else 0 end) as 流失用户数
    from
        (
        SELECT 
            fua.UserID as memberid,
            week(fua.CreateTime) - week(t.created_at) as time,
            Action as 行为
            FROM
                (
                    select user_id, created_at 
                    from user
                    where created_at between Date_format('2019-03-01 00:00:00','%Y-%m-%d') and Date_format('2019-03-07 23:59:59','%Y-%m-%d')
                ) as t
            INNER JOIN user_action fua 
            ON t.user_id = fua.UserID
         where Action is not NULL
    
        ) as t1
    GROUP BY t1.行为
    
    
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决