从一个数据库表中获得访问权限的不同用户的滑动数?

e5njpo68  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(291)

我的表至少包含名字(每个唯一)和上次访问(从epoch开始的秒数):

|   first-name        |    last-access    |    
------------------------------------------
      John                 1592811924
      James                1592810901
      Oliver               1592811924
      Alfred               1592812925
      Alan                 1592813124
      John                 1592813924
      James                1592814040

例如,如何绘制每小时可访问的不同用户数?

5gfr0r5j

5gfr0r5j1#

一个简单的选择是:

select 
    floor(last_access / 60 / 60) * 60 * 60 last_access_hour, 
    count(distinct first_name) no_distinct_users
from mytable
group by last_access_hour

每小时统计不同的用户数。也可以用模运算符表示:

select 
    last_access - last_access % (60 * 60) last_access_hour, 
    count(distinct first_name) no_distinct_users
from mytable
group by last_access_hour

如果要以可读格式显示历元时间戳,可以使用 from_unixtime ```
select
date_format(from_unixtime(last_access), '%Y-%m-%d %H:00:0') last_access_hour,
count(distinct first_name) no_distinct_users
from mytable
group by last_access_hour

相关问题