【问题标题】:Getting count value per minute in postgresql在 postgresql 中获取每分钟的计数值
【发布时间】:2014-06-20 03:28:05
【问题描述】:

根据下面的代码,我希望帮助找出一种可以按分钟计算 max_concrncy 计数的方法。目前的查询每天都会返回计数,我在尝试获取基于分钟的计数时遇到了困难。

SELECT cast(to_char(date,'MM-DD-YYYY') as date) log_dt ,initcap(url),
url,count(userid) tot_hits,count(distinct userid) unq_hits,
count (cast(to_char(date,'MM-01-YYYY hh:mi:00') as date))as max_concrncy 
**// i need max of count (cast(to_char(date,'MM-01-YYYY hh:mi:00') as date)) as max_concrncy here**

,case when httpstatuscode>=100 and httpstatuscode<=199 then httpstatuscode
    else null end as sts_cd_100 
,case when httpstatuscode>=200 and httpstatuscode<=299 then httpstatuscode
    else null end as sts_cd_200
,case when httpstatuscode>=300 and httpstatuscode<=399 then httpstatuscode
    else null end as sts_cd_300
,case when httpstatuscode>=400 and httpstatuscode<=499 then httpstatuscode else null end  as sts_cd_400
,case when httpstatuscode>=500 and httpstatuscode<=599 then httpstatuscode
    else null end as sts_cd_500
FROM sp01.apps_log_temp where initcap(url)='/766/sigma/data.Ashx'
and userid <> '-' group by cast(to_char(date,'MM-DD-YYYY') as date),httpstatuscode

这是查询返回的输出。突出显示的数据是 max_concrncy 值,实际上应该是 25 和 14,这是我在按分钟对值进行分组时计算出来的。

  date             url            tothit unqhit    max_concrncy  httpstatuscode
2014-01-31  /766/sigma/data.Ashx    75  2   **75**          200
2014-01-30  /766/sigma/data.Ashx    50  1   **50**          200

添加输入数据。

    date           httpstatuscode     url
2014-01-31 00:01:51   200       /766/sigma/data.ashx
2014-01-31 00:01:52   200       /766/sigma/data.ashx
2014-01-31 00:01:52   400       /766/sigma/data.ashx
2014-01-31 00:25:57   200       /766/sigma/data.ashx
2014-01-30 00:01:51   300       /766/sigma/data.ashx
2014-01-30 00:01:52   200       /766/sigma/data.ashx
2014-01-30 00:01:52   200       /766/sigma/data.ashx
2014-01-30 00:25:57   200       /766/sigma/data.ashx

抱歉问题这么长。感谢您的答复。

【问题讨论】:

  • 是否可以简化您的问题(我认为您可以删除一些只会增加噪音的列)并提供一些示例数据输入?

标签: postgresql


【解决方案1】:

试试 date_trunc see postgres documentation

那么您不必强制转换或执行 to_char。

【讨论】:

    猜你喜欢
    • 2021-11-25
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    • 2019-07-03
    • 2022-01-11
    • 1970-01-01
    • 2014-06-11
    • 1970-01-01
    相关资源
    最近更新 更多