2016-06-19 9 views
2

私はこのような時間帯で12時間の期間と別の結果を得るためのテーブルからのカウント(*)を選択します:選択クエリ

time   | count 
00:00 - 03:00 | 12 
03:00 - 06:00 | 25 
etc... 

私が好きなものを書いた:

select 
    timestamp, 
    row_number() over (partition by timestamp order by count(*) desc) as score 
from logs 
where to_timestamp(timestamp) between symmetric now() and now() - interval '12 hours' 
group by timestamp 
order by score desc; 

それは、すべてのタイムスタンプ記録に分離:

timestamp  | score 
1466330486.813 | 25 

正しく期間を統一する方法?

SELECT 
    date_trunc('hour', timestamp), 
    count(*) as score 
FROM logs 
GROUP BY date_trunc('hour', timestamp); 

date_truncドキュメントis here:あなたは毎時解像度に慣れている場合

答えて

0

これは、日付機能により、簡単なグループで解決することができます。カスタムの時間帯(現在のタイムスタンプに対して3時間のチャンクや3時間のチャンクなど)が必要な場合は、それを計算するカスタム関数を作成して、同じ方法で処理できます。