SELECT语句作为具有两个条件的子查询

时间:2011-07-23 03:32:47

标签: sql db2

我正在处理基本上具有以下架构的ECG数据

Col 1 = TIMESTAMP
Col 2 = PATIENTID
Col 3 = ECGVALUE

现在,我正在尝试编写一个SQL语句,该语句应该能够选择满足以下条件的所有行

Row index >= n and TIMESTAMP of xth row <= TIMESTAMP of nth row + offset

为了进一步解释,我要说我在数据库中跟踪数据

1.1 ANON 1.1
1.3 ANON 2.3
3.5 ANON 4.3
5.0 ANON 6.5
6.3 ANON 7.5
7.9 ANON 8
8.6 ANON 9.4

现在,我想从第3行选择数据,直到3秒收集数据,这意味着我的结果集应该

3.5 ANON 2.3 *//3rd row till TIMESTAMP <= 3.5 + 3 <= 6.5*
1.3 ANON 2.3
3.5 ANON 4.3
5.0 ANON 6.5
6.3 ANON 7.5

最后两行被忽略,因为第一个和最后一个东西的TIMESTAMP之间的差异超过3.所以如果我回到我的状态,这是

Row index >= n and TIMESTAMP of xth row <= TIMESTAMP of nth row + offset

在这里,

n: nth row from where data must be selected
x: Any arbitary row in result set
offset: Maximum difference between first and last TIMESTAMP of result set.

我已经为上述条件编写了一个有效的SQL语句,但我认为它并没有那么多优化,因为我是SQL的新手。

SELECT TIMESTAMP, ECGVALUE
FROM
(
    SELECT TIMESTAMP, ECGVALUE, ROW_NUMBER() OVER() AS RN
    FROM EKLUND.DEV_RAWECG
)
WHERE RN >= n AND TIMESTAMP <=
(
    SELECT TIMESTAMP FROM
    (
        SELECT TIMESTAMP, ROW_NUMBER() OVER() AS TM
    )
    WHERE TM = n
) + offset;

1 个答案:

答案 0 :(得分:1)

如果使用公用表表达式,代码看起来会更清晰,如下所示:

WITH rownums(TIMESTAMP, ECGVALUE, RN) AS (
    SELECT TIMESTAMP, ECGVALUE, ROW_NUMBER() OVER(ORDER BY TIMESTAMP) AS RN
    FROM EKLUND.DEV_RAWECG
)
SELECT allrows.TIMESTAMP, allrows.ECGVALUE, allrows.RN
FROM rownums allrows
     CROSS JOIN rownums rown
WHERE rown.RN = n
      AND allrows.RN >= n
      AND rown.TIMESTAMP + offset >= allrows.TIMESTAMP;