MySQL - 按唯一登录天数范围内的用户数量和按部门分组

时间:2016-01-18 19:26:13

标签: mysql sql

我正在处理一个查询,它将计算每个用户的唯一登录天数,将它们放入存储桶中(1-2,3-4个登录日等),并返回每个用户的数量按部门划分的每个桶中的用户。如果不太清楚我会道歉,希望下面的例子有助于说明我的问题。

我有一张这样的表:

+-------------+-----------+------------+-----------------+
|    time     | user_name |    dept    |      event      |
+-------------+-----------+------------+-----------------+
| 2016-01-04  | Joe       | finance    | logged in       |
| 2016-01-04  | Jeff      | marketing  | logged in       |
| 2016-01-04  | Jeff      | marketing  | project created |
| 2016-01-04  | Bob       | finance    | logged in       |
| 2016-01-04  | Mark      | accounting | logged in       |
| 2016-01-05  | Bob       | finance    | logged in       |
| 2016-01-08  | Bob       | finance    | logged in       |
| 2016-01-09  | Jeff      | marketing  | logged in       |
| 2016-01-10  | Jeff      | marketing  | logged in       |
| 2016-01-11  | Nate      | accounting | logged in       |
| 2016-01-11  | Nate      | accounting | project created |
+-------------+-----------+------------+-----------------+

我想要返回一个这样的表:

+------------------+-----------------+------------+
| number of logins | number of users |    dept    |
+------------------+-----------------+------------+
| 1-2              |               1 | finance    |
| 3-4              |               1 | finance    |
| 5+               |               0 | finance    |
| 1-2              |               0 | marketing  |
| 3-4              |               1 | marketing  |
| 5+               |               0 | marketing  |
| 1-2              |               2 | accounting |
| 3-4              |               0 | accounting |
| 5+               |               0 | accounting |
+------------------+-----------------+------------+ 

截至目前,我的查询如下:

select 
(case when count(distinct(`time`)) between 1 and 2 then '1-2' 
      when count(distinct(`time`)) between 3 and 4 then '3-4'
      else '5+'
        end) as buckets, dept, user_name
    from change_log where event in ('logged in')  
    group by dept, user_name

然而,这会返回一个如下表格,这是我能够达到的最接近的目标,但我不知道如何将其推广到桶和部门。

+---------+------------+-----------+
| buckets |    dept    | user_name |
+---------+------------+-----------+
| 1-2     | accounting | Mark      |
| 1-2     | accounting | Nate      |
| 3-4     | finance    | Bob       |
| 1-2     | finance    | Joe       |
| 3-4     | marketing  | Jeff      |
+---------+------------+-----------+

3 个答案:

答案 0 :(得分:2)

这样的东西?

select buckets, dept, count(user_name) no_of_u from 
(select 
(case when count(distinct(`time`)) between 1 and 2 then '1-2' 
      when count(distinct(`time`)) between 3 and 4 then '3-4'
      else '5+'
        end) as buckets, dept, user_name
    from change_log where event in ('logged in')  
    group by dept, user_name)
group by buckets, dept

答案 1 :(得分:0)

但你可以这样做:

select
  ELT(LEAST(count(*),5), '1-2', '1-2', '3-4', '3-4','5+') as buckets,
  dept,
  count(*) as `number of users`
FROM change_log where event in ('logged in')
WHERE event in ('logged in')  
GROUP BY
  dept,
  ELT(LEAST(count((*),5), '1-2', '1-2', '3-4', '3-4','5+');

答案 2 :(得分:-1)

为了确保我明白,你基本上都是在尝试这样做:

    select 
    (case when count(distinct(`time`)) between 1 and 2 then '1-2' 
          when count(distinct(`time`)) between 3 and 4 then '3-4'
          else '5+'
     end) as buckets, dept, count(*) as `number of users`
from change_log where event in ('logged in')  
group by dept, buckets

现在你无法在MySQL中做到这一点,因为你不能对案例陈述的输出进行分组,但在功能上可以达到预期的结果吗?