表格包含TIMESTAMP,PATIENTID,ECGVALUE和样本表格如下:
01.34 ANON 12
02.34 ANON 12
03.34 ANON 12
04.34 ANON 12
05.34 ANON 12
06.34 ANON 12
07.34 ANON 12
08.34 ANON 12
10.34 ANON 12
11.34 ANON 12
12.34 ANON 12
现在,我想选择'n'小于或等于最大时间戳的行。从上面的表中我们可以看到,对于PATIENTID ='ANON',最大TIMESTAMP是12.34,现在,我想要这样一个SQL查询,它可以选择所有'n'小于12.34的行,其中n可以是任意数字。到目前为止,我写过这篇文章,但它不起作用。
WITH
rownums(TIMESTAMP, ECGVALUE, RN) AS (
SELECT TIMESTAMP, ECGVALUE ROW_NUMBER() OVER() AS RN
FROM EKLUND.DEV_RAWECG WERE PATIENID = 'ANON'
),
maxtime(MAXTM) AS (
SELECT MAX(TIMESTAMP) AS MAXTM FROM rownums
)
SELECT TIMESTAMP, ECGVALUE, RN FROM rownums
WHERE TIMESTAMP >= maxtime.MAXTM - 2;
答案 0 :(得分:0)
SELECT * FROM `rownums` WHERE (`TIMESTAMP`+2)
< (SELECT MAX(`TIMESTAMP`) FROM `rownums`)
答案 1 :(得分:0)
您不能在where子句中引用maxtime.MAXTM,因为您没有在FROM子句中包含maxtime。相反,您需要更改此
WHERE TIMESTAMP >= maxtime.MAXTM - 2;
到
WHERE TIMESTAMP >= (select MAXTM from maxtime) - 2;