我只想在条件满足时才从表中获取第一行。该表具有多个条目,并且仅当表的顶部存在一列的特定值时,查询才应返回输出,否则查询不应该返回任何行。
SELECT (CASE WHEN COL1 = 'AAA' THEN COL1 ELSE NULL END) AS A
FROM TABLE
QUALIFY ROW_NUMBER () OVER (ORDER BY TIMESTAMP DESC) = 1
WHERE A IS NOT NULL
如果表顶部的列值为'AAA',则应返回输出,否则返回不返回任何行。即使第一行不是“ AAA”,我仍将输出显示为“ AAA”。
答案 0 :(得分:1)
此查询应从表中获取第一行,并查看COL1是否等于“ AAA”。
SELECT * FROM (SELECT * FROM TABLE ORDER BY TIMESTAMP DESC LIMIT 1) WHERE COL1 = 'AAA'
答案 1 :(得分:1)
您必须将WHERE条件移动到QUALIFY:
SELECT *
FROM TABLE
QUALIFY ROW_NUMBER () OVER (ORDER BY TIMESTAMP DESC) = 1
AND COL1 = 'AAA'