sql - Postgresql 在特定时间间隔内创建的记录百分比
问题描述
我有一个包含字段的表created_at
。我想根据指定时间间隔内创建的总数计算记录的百分比。假设我有以下结构:
| name | created_at |
----------------------------------------
| first | "2019-04-29 09:30:07.441717" |
| second | "2019-04-30 09:30:07.441717" |
| third | "2019-04-28 09:30:07.441717" |
| fourth | "2019-04-27 09:30:07.441717" |
所以我想计算在2019-04-28 00:00:00
和之间的时间间隔内创建的记录的百分比是多少2019-04-30 00:00:00
。在这个时间间隔内,我有两条记录first
和third
,所以结果应该是50%
。我遇到了该OVER()
子句,但要么我不知道如何使用它,要么它不是我需要的。
解决方案
您可以使用案例
select 100 * count(case
when created_at between '2019-04-28 00:00:00' and '2019-04-30 00:00:00'
then 1
end) / count(*)
from your_table
推荐阅读
- php - 此代码适用于 php 5.6(服务器状态),为什么它不适用于 php 7.4?
- api - Flutter OCR - 谷歌云视觉 API
- python - 在 Python 中使用元组索引的值对字典进行排序
- android - 此处地图导航不适用于 Android 的画中画模式
- android - Android 模拟器使用的内存超出了应有的范围
- flutter - 如何与在 Dart/Flutter 中执行阻塞操作的隔离体进行通信?
- reactjs - 在material-ui中的List中滚动到一个列表项
- php - 如何从存储为 json 的数据库中检索对象值
- javascript - 在下面的nodejs函数中apply和concat是什么意思,以及如何使用它?
- javascript - CSS - 无法解析本地 ttf 文件?