2017-11-30 2 views
0

私は、PostgreSQLを使用していますし、これは私のテーブルmeasurement_archiveです。私は(指定された時間枠からレコードを取る例えば2017年1月1日0時00分00秒2017から12への問い合わせを必要とする指定された時間枠に1時間ごとにレコードがあるかどうかをチェックし、それを数える方法は?

+-----------+------------------------+------+-------+ 
| sensor_id | time     | type | value | 
+-----------+------------------------+------+-------+ 
| 123  | 2017-11-26 01:53:11+00 | PM25 | 34.32 | 
+-----------+------------------------+------+-------+ 
| 123  | 2017-11-26 02:15:11+00 | PM25 | 32.1 | 
+-----------+------------------------+------+-------+ 
| 123  | 2017-11-26 04:32:11+00 | PM25 | 75.3 | 
+-----------+------------------------+------+-------+ 

-01 23:59:59)、1時間ごとに少なくとも1レコードがあるかどうかを確認します。存在する場合は、結果に1を加えます。

だから、私は2017年11月26日04 2017年11月26日1時○○分00秒から、そのクエリを行う場合:59:上記の表にsensor_id == 123用59 + 00、結果は3でなければなりません。

答えて

1
select count(*) 
from (
    select date_trunc('hour', time) as time 
    from measurement_archive 
    where 
     time >= '2017-11-26 01:00:00' and time < '2017-11-26 05:00:00' 
     and 
     sensor_id = 123 
    group by 1 
) s 
0

別の解決策は、別個の使用であろう

select count(*) from (select distinct a, extract(hour from time) from t where time >'2017-11-26 01:00:11' and time <'2017-11-26 05:00:00' and sensor_id=123)t; 
関連する問題