2016-08-20 3 views
1

は:列に登録された異なる値の合計を返すクエリを作成しますか?私はテーブル・イベントがある場合

create table events (
     sensor_id integer not null, 
     event_type integer not null, 
     value float not null, 
     time timestamp not null 
); 

を私は別の種類の数とsensor_idのすべてを返すクエリを作成したいです。

例えば:

sensor_id | event_type | value  | time 
    -----------+------------+------------+-------------------- 
    2   | 2   | 3.45  | 2014-02-13 12:42:00 
    2   | 4   | -149.42 | 2014-02-13 12:42:00 
    2   | 2   | 4.12  | 2014-02-13 12:54:39 
    3   | 2   | 3.47  | 2014-02-13 12:42:00 
    2   | 3   | 24.54  | 2014-02-13 13:32:36 

が返す:

sensor_id | types 
    -----------+----------- 
    2   | 3 
    3   | 1 

答えて

2

あなたはそれを行うためにGROUP BYCOUNT(DISTINCT)を使用することができます。

SELECT sensor_id, COUNT(DISTINCT event_type) AS types 
FROM events 
GROUP BY sensor_id 
関連する問題