2017-08-21 45 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万行,那么这个查询需要花费大量的时间。不知道还有多少时间,还在运行它。 –