Jmesः:
私は、「テスト」と呼ばれるのMySQL(10.1.44-MariaDB)に「raw_temp」と呼ばれるテーブルを持っています。
これは、次の形式でセンサ値を保持し、残念ながら私は、形式について何もすることはできません。
+----+--------+--------+--------+--------+--------+--------+---------------------+
| id | api | value1 | value2 | value3 | value4 | value5 | reading_time |
+----+--------+--------+--------+--------+--------+--------+---------------------+
| 1 | asdasd | 1968 | 19.70 | 52.50 | NULL | NULL | 2020-03-02 21:34:46 |
| 2 | asdasd | NULL | NULL | NULL | 100 | NULL | 2020-03-02 21:35:46 |
| 3 | asdasd | 1974 | 19.70 | 52.50 | NULL | NULL | 2020-03-02 21:37:47 |
| 4 | asdasd | NULL | NULL | NULL | NULL | 88 | 2020-03-02 21:39:05 |
| 5 | xdfsgh | 2543 | NULL | NULL | NULL | NULL | 2020-03-02 21:39:49 |
+----+--------+--------+--------+--------+--------+--------+---------------------+
reading_timeは、「2020年3月2日夜9時35分01秒」と「2020年3月2日21時40分00秒」の間にあるところ私は、SELECTクエリの次出できるようにしたいと思います。Naimly一つずつ「API」値の行、および最後の非NULL読み取り値を示す値列(およびなし表示NULLがある場合)、値、その値に関連付けられreading_timeを示すタイムカラムを読み出す(うまくいけば、この表に伴い以下)理にかなっています。
これは、上記の?(私も、私は推測するループとリストと概要を(作成するためのPythonを使用するオプションを持っている)MySQLのSELECT文の中で最初にしても可能である例であり、実際にセンサ値の約2000行があると約150のAPI値、およびIは、理想的には次の5分間隔の前に他の処理のための時間を与えるために、<1分で実行するクエリたいと思います。
+--------+--------+---------------------+--------+---------------------+--------+---------------------+--------+----------------------+--------+----------------------+
| api | value1 | value1_reading_time | value2 | value2_reading_time | value3 | value3_reading_time | value4 | value4_reading_time | value5 | value5_reading_time |
+--------+--------+---------------------+--------+---------------------+--------+---------------------+--------+----------------------+--------+----------------------+
| asdasd | 1974 | 2020-03-02 21:37:47 | 19.70 | 2020-03-02 21:37:47 | 52.50 | 2020-03-02 21:37:47 | 100 | 2020-03-02 21:35:46 | 88 | 2020-03-02 21:39:05 |
| xdfsgh | 2543 | 2020-03-02 21:39:49 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
+--------+--------+---------------------+--------+---------------------+--------+---------------------+--------+----------------------+--------+----------------------+
GMB:
あなたは、MySQL 8.0を実行している場合は、それぞれの値の入手の最後の日付を計算するために、サブクエリで窓関数を使用して、条件付きの集約を行うことができます。
ここでは三つの値の例です:
select
api,
max(case when reading_time = max_reading_time_1 then value1 end) value1,
max_reading_time_1,
max(case when reading_time = max_reading_time_2 then value2 end) value2,
max_reading_time_2,
max(case when reading_time = max_reading_time_3 then value3 end) value3,
max_reading_time_3
from (
select
t.*,
max(case when value1 is not null then reading_time else end)
over(partition by api) max_reading_time_1,
max(case when value2 is not null then reading_time else end)
over(partition by api) max_reading_time_2,
max(case when value3 is not null then reading_time else end)
over(partition by api) max_reading_time_3
from t
) t
where reading_time in (max_reading_time_1, max_reading_time_2, max_reading_time_3)
group by api