簡體   English   中英

僅在SQL Server中某個相應的列條件='y'之后才查詢列條件='x'的行

[英]Query row where column condition = 'x' only after a certain corresponding column condition = 'y' in SQL Server

如果我缺少重要信息,請提前道歉,這是我的新手。

我有一個龐大的設備,接收者數據庫和相應的值數據庫,這些值在收到消息時就累加一。

例如,這是我的原始數據表

+-----------------------+--------+-------+------------+-------+
|received_time          |receiver|device |message_type| value | 
+-----------------------+--------+-------+------------+-------+                                          
|2019-07-17 7:50:14.850 | R1     |     D1|      2     |   1   |
|2019-07-17 7:50:14.853 | R1     |     D1|      1     |  NULL | 
|2019-07-17 7:50:14.947 | R1     |     D1|      2     |   1   |
|2019-07-17 7:50:14.957 | R1     |     D1|      1     |  NULL |
|2019-07-17 7:50:15.060 | R2     |     D1|      2     |   1   | 
|2019-07-17 7:50:15.067 | R3     |     D1|      2     |   1   |
|2019-07-17 7:50:15.073 | R3     |     D1|      1     |  NULL |
|2019-07-17 7:50:15.170 | R4     |     D2|      1     |  NULL |
|2019-07-17 7:50:15.283 | R5     |     D2|      2     |   0   |
|2019-07-17 7:50:15.287 | R5     |     D2|      1     |  NULL |
|2019-07-17 7:55:00.000 | R1     |     D2|      5     |  NULL |  
|2019-07-17 7:55:01.400 | R1     |     D2|      2     |   A   |
+-----------------------+--------+-------+------------+-------+

and so on...

目的

我的目標是僅在列出了特定的message_type情況下查詢相應的值。 問題是,該值在另一行上。 這可能是message_type = '5'message_type = '1' (無論我目前在尋找什么)。 問題是,我唯一想到的方法是通過receive_time進行排序,但要在滿足初始message_type條件后的x秒內進行排序,但是在某些情況下,“ 2”要比相應的“ 1”早message_type

我已經嘗試過GROUP BY函數的不同變體,但這非常令人困惑,我不確定從這里開始。

嘗試1-不好

SELECT received_time, receiver, device, message_type, value,
    CASE
        WHEN message_type = '5' THEN --Select corresponding value 
                                     --where message_type = '2'
    END 
FROM table;

嘗試#2-接近我要去做的事

SELECT * FROM my_table
    WHERE message_type = '5' 
        SELECT * FROM my_table
            WHERE
                received_time BETWEEN (received_time - 3 seconds) 
                and (received_time + 3 seconds)
                and (device = device) --order by for each device?
                and (message_type = '5' or message_type = '2')

如果我想搜索message_type = 5,則我的預期表應如下所示:

+-----------------------+--------+-------+------------+-------+
|received_time          |receiver|device |message_type| value | 
+-----------------------+--------+-------+------------+-------+                                          
|2019-07-17 7:55:00.000 | R1     |     D2|      5     |  NULL |
|2019-07-17 7:55:01.400 | R1     |     D2|      2     |   A   | 
+-----------------------+--------+-------+------------+-------+

但是如果我想搜索message_type = 1,則預期表應該看起來像這樣:

+-----------------------+--------+-------+------------+-------+
|received_time          |receiver|device |message_type| value | 
+-----------------------+--------+-------+------------+-------+                                          
|2019-07-17 7:50:14.850 | R1     |     D1|      2     |   1   |
|2019-07-17 7:50:14.853 | R1     |     D1|      1     |  NULL | 
|2019-07-17 7:50:15.170 | R4     |     D2|      1     |  NULL |
|2019-07-17 7:50:15.283 | R5     |     D2|      2     |   0   |
+-----------------------+--------+-------+------------+-------+

這是一個非常棘手的問題,很難解釋。 非常感謝您的幫助,謝謝!

據我了解,您的問題是您需要為每個message_type 1或5找到對應的message_type 2並獲取值,並考慮到類型2可能在類型1或5之前和之后。我在這里提出的解決方案認為對應的類型2是最接近原始消息的類型(類型1或5)。

我分兩個步驟進行操作:

  1. 計算哪種類型2最接近我要查找的類型:

    選擇t2.receiver,t2.device,t2.received_time,MIN(ABS(DATEDIFF(MILLISECOND,t.received_time,t2.received_time))))AS最接近時間INTO #tmp距離my_table最近,因為t內部加入my_table為t2 ON t.receiver = t2.receiver和t.device = t2.device,其中t.message_type = 2 AND t2.message_type = 1-在此定義主消息的類型(1或5)GROUP BY t2.receiver,t2.device,t2。接收時間

  2. 使用此臨時表,您可以檢索對應的一個臨時表的值。

    SELECT tc.receiver,tc.device,tc.received_time,t.value from #tmpClosest AS tc INNER JOIN my_table AS t ON t.receiver = tc.receiver AND t.device = tc.device WHERE tc.closest_received_time = ABS(DATEDIFF (MILLISECOND,t.received_time,tc.received_time))

暫無
暫無

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

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