【问题标题】:Calculate Running total of active users by groups按组计算活动用户的运行总数
【发布时间】:2019-08-29 14:52:09
【问题描述】:

我需要根据组找到系统中活跃用户的运行总数 如果用户当前被暂停,则不应在当前计数中考虑他,但应在他处于活动状态时考虑 对于前 一个用户在 2019 年 4 月被暂停,所以他应该被视为活跃用户,直到 3 月份的所有计数结束

users 表包含状态 ('Active'/'Suspended') 和 updatedon 列 我已经编写了代码,但它并没有给我想要的结果,因为它只考虑了当前的活跃用户

 SELECT DISTINCT Concat(year(b1.created_at),'-',monthname(b1.created_at)) Date,c1.name as 'GroupName',
(   
    select 
    count(distinct(b.user_id))      from 
        users u2 join teams_users b on u2.id = b.user_id join teams c on b.team_id = c.id  
    where 
    year(b.created_at)*100 + month(b.created_at) <= year(b1.created_at)*100+month(b1.created_at)
     and 
    u2.organization_id =1690 
             and u2.status = 'Active' 
    and c.organization_id = 1690 
     and c.id =c1.id
    ) total_users
 from users u1 join teams_users b1 on u1.id = b1.user_id 
 join teams c1 on b1.team_id = c1.id
 where 
     u1.organization_id =1690  and u1.status = 'Active'

用户

 UserId|CreatedOn|Status|Updatedon

团队

 Id|Name

Team_users

 Team_id|User_id|CreatedOn

【问题讨论】:

  • 它只考虑当前活跃用户,因为这两个查询都明确声明where ... and u1/u2.status = 'Active' 所以当然你只会找到活跃用户。您不能在 where 子句中使用它并期望找到有关当前非活动用户的记录。
  • 是的,但如果我删除状态并且用户已被暂停,我如何将其从进一步添加中删除
  • 在不知道数据结构的情况下很难回答这个问题,但是您可以使用 case 语句在活动时将“活动”计数加 1,否则加 0。同样,在不活动计数时加 1未激活,否则加 0,查找文档以获取案例语句,它们非常有帮助。
  • 您能否至少发布您正在使用的数据结构和您编写的真实查询?发布的那个是错误的,表别名都搞砸了(b1c1 不存在),因此不可能弄清楚你在做什么,例如带有created_at 条件。
  • @MarcinJ 已更新

标签: mysql sql cumulative-sum


【解决方案1】:

下面是一个示例查询,它将为您提供给定月份、每个月、每个团队的用户总数:

with generator as
(
SELECT 0 n UNION ALL SELECT 1  UNION ALL SELECT 2  UNION ALL 
   SELECT 3   UNION ALL SELECT 4  UNION ALL SELECT 5  UNION ALL
   SELECT 6   UNION ALL SELECT 7  UNION ALL SELECT 8  UNION ALL
   SELECT 9   UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL
   SELECT 12  
),
user_to_team as
(
   select user_id
        , team_id
        , tu.created_at as date_joined
        , case 
            when u.status = 'suspended'
            then u.updatedon
            else curdate()
          end member_until
     from team_users tu
     join users u
       on u.id = tu.user_id
), periods as
(
   select year*100 + month as period
     from (select 2018+n as year from generator where n < 2) x
    cross
     join (select 1+n as month from generator where n<12) y
)
select periods.period
     , t.id
     , count(u2t.user_id)
  from periods
  join teams t
    on year(t.created_at) * 100 + month(t.created_at) <= periods.period
  left outer
  join user_to_team u2t
    on periods.period between year(u2t.date_joined)*100 + month(u2t.date_joined) and year(u2t.member_until)*100 + month(u2t.member_until)
   and u2t.team_id = t.id
 where periods.period <= year(curdate())*100 + month(curdate())
group by periods.period, t.id
order by periods.period, t.id

generator CTE 用于生成数字,我们需要在 periods 中生成年月列表 (yyyymm)。如果你有一个numbers 表,你可以使用它。 user_to_team CTE 仅列出用户加入时(记录在user_teams 创建)和直到他们成为成员(如果他们被暂停,则为用户的updatedon 或当前日期)之间的关系。然后,只需将我们的periods 加入到teamsuser_to_team 加入到基于日期的那个中 - 团队自创建以来就加入了,成员也加入了他们存在的时期。

Here's a working sample on dbfiddle

有两个团队:

  • 团队 1 创建于 2019-01
  • 团队 2 创建于 2019-02

用户:

  • 用户 1,活跃
  • 用户 2,自 2019 年 3 月起暂停

团队到用户:

  • 用户 1 自 2019 年 1 月起加入团队 1,自 2019 年 3 月起加入团队 2
  • 用户 2 自 2019 年 3 月起加入团队 1,自 2019 年 3 月起加入团队 2

【讨论】:

    猜你喜欢
    • 2019-10-27
    • 1970-01-01
    • 2016-08-31
    • 1970-01-01
    • 1970-01-01
    • 2023-01-06
    • 2014-04-05
    • 2021-10-01
    • 2012-05-21
    相关资源
    最近更新 更多