表:Sessions
+---------------------+---------+ | Column Name | Type | +---------------------+---------+ | session_id | int | | duration | int | +---------------------+---------+ session_id 是该表主键 duration 是用户访问应用的时间, 以秒为单位
你想知道用户在你的 app 上的访问时长情况。因此你决定统计访问时长区间分别为 "[0-5>"
,"[5-10>"
,"[10-15>"
和 "15 minutes or more"
的会话数量,并以此绘制柱状图。
写一个解决方案来报告 (bin, total)
。
返回结果 无顺序要求 。
结果格式如下所示。
示例 1:
输入: Sessions 表: +-------------+---------------+ | session_id | duration | +-------------+---------------+ | 1 | 30 | | 2 | 199 | | 3 | 299 | | 4 | 580 | | 5 | 1000 | +-------------+---------------+ 输出: +--------------+--------------+ | bin | total | +--------------+--------------+ | [0-5> | 3 | | [5-10> | 1 | | [10-15> | 0 | | 15 or more | 1 | +--------------+--------------+ 解释: 对于 session_id 1,2 和 3 ,它们的访问时间大于等于 0 分钟且小于 5 分钟。 对于 session_id 4,它的访问时间大于等于 5 分钟且小于 10 分钟。 没有会话的访问时间大于等于 10 分钟且小于 15 分钟。 对于 session_id 5, 它的访问时间大于等于 15 分钟。
SELECT '[0-5>' AS bin, COUNT(1) AS total FROM Sessions WHERE duration < 300
UNION
SELECT '[5-10>' AS bin, COUNT(1) AS total FROM Sessions WHERE 300 <= duration AND duration < 600
UNION
SELECT '[10-15>' AS bin, COUNT(1) AS total FROM Sessions WHERE 600 <= duration AND duration < 900
UNION
SELECT '15 or more' AS bin, COUNT(1) AS total FROM Sessions WHERE 900 <= duration;