sql - 在特定时间间隔内创建的 Postgresql 记录百分比

标签 sql postgresql percentage

我有一个包含字段 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:002019-04-30 00:00 之间的时间间隔内创建的记录的百分比: 00。在这个时间间隔内,我有两条记录firstthird,所以结果应该是50%。我遇到了 OVER() 子句,但要么我不知道如何使用它,要么它不是我需要的。

最佳答案

你可以使用 CASE

 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

关于sql - 在特定时间间隔内创建的 Postgresql 记录百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55901253/

相关文章:

sql - 将多行合并为多列

sql - Azure Synapse Analytics(以前称为 SQL SW)与 Azure Synapse Analytics(工作区预览)

c# - NpgSQL插入包含反斜杠的文件路径 "\\"

ruby-on-rails - 尝试在 Heroku 上的 Rails 中将价格设置为扩展十进制/BTC

command - Hive计算百分比

html - 可滚动内容到百分比大小的 div 中

sql - 为什么比较在 CONVERT 中不起作用?

postgresql - docker postgresql 容器的循环备份

postgresql - 在 Postgres 中按值计算组的百分比值

sql - 计算记录分组的所有现有组合