SQL更新查询嵌套/连接选择

时间:2017-08-21 13:24:24

标签: sql-server join sql-update

我有这两张桌子:

表1

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

表2

table2.time_stamp

我想在这种情况下更新表1:

table1.time_stamp < table2.time_stamp

第一次出现previousNumberByTimeStampnumber (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'

我遇到click()的问题。 如何从表中获取该信息?

这是我想要为这个例子实现的结果:

表1

@Before
public void setUp() throws Exception 
{
    driver = new FirefoxDriver();
    baseUrl = "https://www.example.com/";
}

@Test
public void OrderWithLoginFbCash() throws Exception
{
    driver.get(baseUrl);

    // remembers the parent Windowhandle
    String parentHandle  = driver.getWindowHandle(); 

    // Opens Facebook login window
    WebElement FbLogin = (new WebDriverWait(driver , 20)).until(
        ExpectedConditions.presenceOfElementLocated(By.id("fblogin"))
    );
    FbLogin.click();

    for (String winHandle : driver.getWindowHandles()){
        driver.switchTo().window(winHandle);
    }

    // ... filling up the inputs ...

    WebElement FbLoginFacebook = (new WebDriverWait(driver, 20)).until(
        ExpectedConditions.presenceOfElementLocated(By.id("u_0_0"))
    );
    // everything's still fine here
    FbLoginFacebook.click(); // logs in, closes the window and reloads the page

    // !!! Test won't continue here for some reason. !!!

    System.out.println("Did the logging in stuff"); // Won't be printed

}

1 个答案:

答案 0 :(得分: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