在postgresql中获取每分钟的计数值

时间:2014-06-12 06:40:11

标签: postgresql

根据下面的代码,我想帮助找出一种可以在一分钟内计算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

对不起这个问题很久了。感谢您的答复。

1 个答案:

答案 0 :(得分:0)

尝试date_trunc see postgres documentation

然后你不必演员或做to_char。