sql - 如何计算每秒的记录数?

标签 sql postgresql

我在数据库 postgresql 的字段跟踪(不带时区的时间戳)中有以下一组值:

"2017-02-22 18:46:43.394"
"2017-02-22 18:46:43.316"
"2017-02-22 18:46:43.237"
"2017-02-22 18:46:42.011"
"2017-02-22 18:46:41.927"
"2017-02-22 18:46:41.728"

我想按秒计算出现次数 (COUNT) 的平均值。在这个例子中是:

18:46:43 > 3 occurrences
18:46:42 > 1 occurrences
18:46:41 > 2 occurrences

AVERAGE = 2 occurrences per second

提案:

SELECT AVG(COUNT(trace???))
FROM log_data

最佳答案

显示这段代码:

select
count(date_trunc('sec',exe.x)),
date_trunc('sec',exe.x)
from
(
    select
    unnest(
        array
        [
        '2017-02-22 18:46:43.394'::timestamp,
        '2017-02-22 18:46:43.316'::timestamp,
        '2017-02-22 18:46:43.237'::timestamp,
        '2017-02-22 18:46:42.011'::timestamp,
        '2017-02-22 18:46:41.927'::timestamp,
        '2017-02-22 18:46:41.728'::timestamp
        ]
    ) as x
) exe
group by date_trunc('sec',exe.x)

关于sql - 如何计算每秒的记录数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42412515/

相关文章:

sql - 如何通过sql查询解决复杂的group?

sql - 如何准确地将nvarchar转换为varbinary

sql - 如何在 Oracle SQL Developer 中查看 blob 数据

postgresql - 将 View 名称作为参数传递给函数

arrays - PostgreSQL : How to create a constraint to check an element is not in an array of string

postgresql - lo_export 在 PostgreSQL 9.3 中不存在

sql - 无法更改 View 列 SQL 的数据类型

mysql - 为什么这个查询需要这么长时间才能执行

mysql - 在mysql中将列转置为行

postgresql - 为什么我不能登录到 Postgres?