2017-08-21 74 views
1

我有這2個表:SQL更新查詢嵌套/連接選擇

表1

number (int) | time_stamp (datetime2) | number2 (int) | time_stamp2 (datetime2) 
-------------------------------------------------------- 
20   | '2017-08-01 01:00:00' | null   | null 
100   | '2017-08-01 01:00:00' | null   | null 

表2

number (int) | time_stamp (datetime2) 
-------------------------- 
50   | '2017-08-01 01:01:01' 
70   | '2017-08-01 01:01:02' 
80   | '2017-08-01 01:01:03' 
102   | '2017-08-01 01:01:04' 
100   | '2017-08-01 01:01:05' 
140   | '2017-08-01 01:01:06' 
200   | '2017-08-01 01:01:07' 
50   | '2017-08-01 01:01:08' 
300   | '2017-08-01 01:01:09' 
400   | '2017-08-01 01:01:10' 

我想在那種情況下更新表1:

update table1 set number2 = table2.number2, time_stamp2 = table2.timestamp 
    where table1.number - table2.number <= -50 and table2.number - table2.(previousNumberByTimeStamp) >= 30 

第一次出現table2.time_stamptable1.time_stamp < table2.time_stamp。我的問題是previousNumberByTimeStamp。 如何從表中獲取該信息?

這就是我要實現該示例中的結果:

表1

number (int) | time_stamp (datetime2) | number2 (int) | time_stamp2 (datetime2) 
-------------------------------------------------------- 
20   | '2017-08-01 01:00:00' | 140   | '2017-08-01 01:01:06' 
100   | '2017-08-01 01:00:00' | 200   | '2017-08-01 01:01:07' 
+0

你能試着在這裏解釋你的商業規則嗎?我已經閱讀過幾次,每次閱讀它都會對我越來越不感興趣。 –

+0

是的,我會盡力解釋一下:你需要在第二個表格中找到一個數字,例如他以前的數字在時間標記上有一個X數字的差別。同樣的數字與表1中的數字有不同。那麼你需要將該數字設置爲table1的number2和他的時間戳。 –

回答

0

檢查此版本:

DECLARE @table1 TABLE 
(number int, time_stamp datetime2, number2 int, time_stamp2 datetime2); 

INSERT @table1 (number,time_stamp) 
VALUES (20, '2017-08-01 01:00:00'), 
     (100, '2017-08-01 01:00:00'); 

DECLARE @table2 TABLE 
(number int, time_stamp datetime2); 

INSERT @table2 (number,time_stamp) 
VALUES (50 ,'2017-08-01 01:01:01'), 
     (70 ,'2017-08-01 01:01:02'), 
     (80 ,'2017-08-01 01:01:03'), 
     (102,'2017-08-01 01:01:04'), 
     (100,'2017-08-01 01:01:05'), 
     (140,'2017-08-01 01:01:06'), 
     (200,'2017-08-01 01:01:07'), 
     (50 ,'2017-08-01 01:01:08'), 
     (300,'2017-08-01 01:01:09'), 
     (400,'2017-08-01 01:01:10'); 

WITH cte AS 
(
    SELECT number, time_stamp, lag(number) over(ORDER BY time_stamp) prev 
     FROM @table2 
) 
UPDATE t1 
SET t1.number2 = x.number, 
    t1.time_stamp2 = x.time_stamp 
    FROM @table1 t1 
    OUTER APPLY (
        SELECT top(1) cte.number, cte.time_stamp 
         FROM cte 
         WHERE t1.time_stamp < cte.time_stamp 
          AND t1.number - cte.number <= -50 
          and cte.number - cte.prev >= 30 
         ORDER BY cte.time_stamp 
       ) AS x; 

SELECT * FROM @table1; 

輸出:

number  time_stamp     number2  time_stamp2 
----------- --------------------------- ----------- --------------------------- 
20   2017-08-01 01:00:00.0000000 140   2017-08-01 01:01:06.0000000 
100   2017-08-01 01:00:00.0000000 200   2017-08-01 01:01:07.0000000 
+0

看起來很棒,我要在我的大數據上測試它......希望它能夠快速運行。一旦我看到它的工作,因爲我想將它標記爲已回答。 –

+0

@MoranBarzilay,你是否測試過大數據腳本的性能? –

+0

是的,它很糟糕。比方說,table1有100行,table2有1000萬行,那麼這個查詢需要花費大量的時間。不知道還有多少時間,還在運行它。 –