leetcode 1435 制作会话柱状图(postgresql)

需求

表:Sessions

±--------------------±--------+
| Column Name | Type |
±--------------------±--------+
| session_id | int |
| duration | int |
±--------------------±--------+
session_id 是该表主键
duration 是用户访问应用的时间, 以秒为单位

你想知道用户在你的 app 上的访问时长情况。因此决定统计访问时长区间分别为 “[0-5>”, “[5-10>”, “[10-15>” 和 “15 or more” (单位:分钟)的会话数量,并以此绘制柱状图。

写一个SQL查询来报告(访问时长区间,会话总数)。结果可用任何顺序呈现。

下方为查询的输出格式:

Sessions 表:
±------------±--------------+
| session_id | duration |
±------------±--------------+
| 1 | 30 |
| 2 | 199 |
| 3 | 299 |
| 4 | 580 |
| 5 | 1000 |
±------------±--------------+

Result 表:
±-------------±-------------+
| 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 分钟。

输入

在这里插入图片描述

输出

with t1 as (
select session_id,duration/60 as du1
from sessions
)
select '[0-5>' as bin , count(1) as total from t1 where du1<5
union all
select '[5-10>' as bin , count(1) as total from t1 where du1>=5 and du1<10
union all
select '[10-15>' as bin , count(1) as total from t1 where du1>=10 and du1<15
union all
select '15 or more' as bin , count(1) as total from t1 where du1>=15

在这里插入图片描述

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值