簡體   English   中英

MySql 各個列的最后一個非 NULL 值和行日期時間

[英]MySql last non-NULL value for various columns and the row datetime

我在名為“test”的 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 |
+----+--------+--------+--------+--------+--------+--------+---------------------+

我希望能夠從 SELECT 查詢中獲得以下內容,其中 reading_time 介於 "2020-03-02 21:35:01" 和 "2020-03-02 21:40:00" 之間。 每個“api”值只有一行,值列顯示最后一個非 NULL 讀取值(如果沒有顯示 NULL)和值讀取時間列顯示該值的關聯 reading_time(希望這與表一起)下面是有道理的)。

這甚至可能首先在 MySQL SELECT 語句中使用(我也可以選擇使用 Python 來創建摘要(我猜是循環和列表)?上面是一個例子,實際上有大約 2000 行傳感器值和大約 150 個 api 值,我希望查詢在 <1 分鍾內運行,以便在下一個 5 分鍾間隔之前有時間進行其他處理。

+--------+--------+---------------------+--------+---------------------+--------+---------------------+--------+----------------------+--------+----------------------+ 
|  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                 |
+--------+--------+---------------------+--------+---------------------+--------+---------------------+--------+----------------------+--------+----------------------+

如果您運行的是 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM