我有一张如下表,
并且我试图在第一次出现某个键的特定事件+包含该事件的行之后获取所有行,如下面的“xn”(column1),
你见过QUALIFY()函数吗?在这里与MIN()结合使用,并在您所需的键上进行窗口化,效果很好。
此外,如果您想要更快的答案,有时提供用于测试和运行您的问题的 cte 可能会加快答案。
复制|粘贴|运行
WITH CTE AS
(SELECT TO_TIMESTAMP('9/1/2021 10:10','MM/DD/YYYY HH24:MI') AS TIMESTAMP , 'A' KEY ,'XA' AS COLUMN_1
UNION
SELECT TO_TIMESTAMP('9/1/2021 10:11','MM/DD/YYYY HH24:MI') AS TIMESTAMP , 'A' KEY,'XN' AS COLUMN_1
UNION
SELECT TO_TIMESTAMP('9/1/2021 10:12','MM/DD/YYYY HH24:MI') AS TIMESTAMP , 'A' KEY,'XN' AS COLUMN_1
UNION
SELECT TO_TIMESTAMP('9/1/2021 10:13','MM/DD/YYYY HH24:MI') AS TIMESTAMP , 'A' KEY,'XD' AS COLUMN_1
UNION
SELECT TO_TIMESTAMP('9/1/2021 10:14','MM/DD/YYYY HH24:MI') AS TIMESTAMP , 'A' KEY,'XT' AS COLUMN_1
UNION
SELECT TO_TIMESTAMP('9/1/2021 10:15','MM/DD/YYYY HH24:MI') AS TIMESTAMP , 'A' KEY,'XM' AS COLUMN_1
UNION
SELECT TO_TIMESTAMP('9/1/2021 10:16','MM/DD/YYYY HH24:MI') AS TIMESTAMP , 'A' KEY,'XJ' AS COLUMN_1
UNION
SELECT TO_TIMESTAMP('9/1/2021 10:12','MM/DD/YYYY HH24:MI') AS TIMESTAMP , 'B' KEY,'XR' AS COLUMN_1
UNION
SELECT TO_TIMESTAMP('9/1/2021 10:13','MM/DD/YYYY HH24:MI') AS TIMESTAMP , 'B' KEY,'XU' AS COLUMN_1
UNION
SELECT TO_TIMESTAMP('9/1/2021 10:14','MM/DD/YYYY HH24:MI') AS TIMESTAMP , 'B' KEY,'XN' AS COLUMN_1
UNION
SELECT TO_TIMESTAMP('9/1/2021 10:15','MM/DD/YYYY HH24:MI') AS TIMESTAMP , 'B' KEY,'XI' AS COLUMN_1
UNION
SELECT TO_TIMESTAMP('9/1/2021 10:16','MM/DD/YYYY HH24:MI') AS TIMESTAMP , 'B' KEY,'XH' AS COLUMN_1 )
SELECT
*
FROM
CTE
QUALIFY
TIMESTAMP >= MIN(DECODE(COLUMN_1,'XN', TIMESTAMP ,NULL) ) OVER
(PARTITION BY KEY ORDER BY TIMESTAMP)