sql - 如何从一个纪元中提取一天中的小时并计算该小时内发生的每个实例?
问题描述
我有一个我觉得很直截了当的问题,但给我带来了一些问题。
我在表 X 中有一个名为的列,event_time
它是一个纪元。我想从中提取一天中的时间并计算在那一小时内发生的骑行次数。
所以输出最终将是一个条形图,其中 x 值为 0-24,Y 是发生的实例数(例如骑自行车)。
这是我现在所拥有的,它没有给我正确的输出:
select extract(hour from to_timestamp(start_time)::date) as hr,
count(*) as ct
from x
group by hr
order by hr asc
任何提示或帮助表示赞赏。
谢谢
解决方案
您可以使用算术:
select floor( (start_time % (24 * 60 * 60)) / (60 * 60) ) as hour,
count(*)
from x
group by hour;
或转换为日期/时间并提取小时:
select extract(hour from '1970-01-01'::date + start_time * interval '1 second') as hour, count(*)
from x
group by hour;
推荐阅读
- python - Pymodbus:在 StopServer -> StartSerialServer 之后不起作用(无法打开端口:PermissionError)
- intellij-idea - 我无法在 IntelliJ 上编辑 http 文件或使用 http 客户端,我该如何解决?
- optimization - 如何在 F# 中优化此代码以提高速度,以及为什么一个部分执行两次?
- c# - Web服务异步调用冻结页面
- azerothcore - 新死亡骑士不会开启符文锻造
- unit-testing - Azure DevOps:防止在多个单元测试项目中覆盖 codecov 报告
- python - 获取 TypeError:forward() 缺少 1 个必需的位置参数:Learner() 上的“x”。快速 AI 的预测
- testing - 如何使用使用 redis 的 easycache 编写 xunit 测试?
- excel - 将单元格值添加到 URL 以查找数据 vba
- spring-boot - 超出 VALUE_STRING 令牌异常