0
点赞
收藏
分享

微信扫一扫

每一题-93(制作会话柱状图)

慎壹 2022-02-04 阅读 52
sql

题93:

根据下表写一个SQL查询来报告(访问时长区间,会话总数),结果可用任何顺序呈现。
其中:决定统计访问时长区间分别为 “[0-5>”, “[5-10>”, “[10-15>” 和 “15 or more” (单位:分钟)的会话数量,并以此绘制柱状图。
在这里插入图片描述
其中:session_id 是该表主键,duration 是用户访问应用的时间, 以秒为单位。

解题思路:用union将每个区间连接,统计每个区间的个数即可。

select '[0-5>'  bin,count(duration)  total
from Sessions
where duration < 300
union
select '[5-10>'  bin,count(duration)  total
from Sessions
where duration >= 300 and duration < 600
union
select '[10-15>'  bin,count(duration)  total
from Sessions
where duration >= 600 and duration < 900
union
select '15 or more'  bin,count(duration)  total
from Sessions
where duration > 900;
举报

相关推荐

0 条评论