Teradata SQL - 在一列中找到最后一个匹配项,然后从下面的行中找到 return 值
Teradata SQL - find the last match in a column then return value from row below
我有一个如下所示的 table:我已经按时间、ID 订购了 table。我想为每个 ID 找到最后一个事件 44 并获取它下面的值。
Table:
ID Time Event
A 00:10:00 11
A 00:10:11 44
A 00:10:13 22
A 00:10:00 11
A 00:10:11 44
A 00:10:13 33
A 00:10:13 22
B 01:10:00 ?
B 01:10:11 44
B 01:10:13 11
B 01:10:00 44
B 01:10:11 ?
B 01:10:13 22
... ... ...
想要的结果:
ID Time Event
A 00:10:13 33
B 01:10:13 22
... ... ...
已经尝试了不同的方法,但仍在努力...任何帮助将不胜感激。非常感谢!
假设您有一列实际上表示您确实想要下一行的顺序 和 ,这是一种方法:
select t.*
from (select t.*,
max(case when event = 44 then next_ordercol end) over (partition by id order by ordercol) as event44_next
from (select t.*,
lead(ordercol) over (partition by id order by ordercol) as next_ordercol
from t
) t
) t
where ordercol = event44_next;
我总是忘记 Teradata 没有实现 lead()
和 lag()
。我真的不明白。这是一种方法:
from (select t.*,
max(ordercol) over (partition by id order by ordercol rows between 1 following and 1 following) as next_ordercol
from t
) t
这将导致解释中的单个 STAT FUNCTION 步骤:
SELECT *
FROM tab
WHERE event IS NOT NULL
QUALIFY
-- previous event was 44
Min(event) -- LEAD
Over (PARTITION BY id
ORDER BY "time" DESC
ROWS BETWEEN 1 Following AND 1 Following) = 44
AND
-- only return rows when there was no previous 44 event
Count(CASE WHEN event = '44' THEN 1 end)
Over (PARTITION BY id
ORDER BY "time" DESC ROWS Unbounded Preceding) = 0
例如
ID TIME Event Lead Count
A 00:11:14 22 33 0
A 00:11:13 33 44 0
A 00:11:11 44 11 1
A 00:11:00 11 22 1
A 00:10:13 22 44 1
A 00:10:11 44 11 2
如果你 运行 TD16+ 终于支持 LAG/LEAD 而不是 MIN 重写:
Lead(event)
Over (PARTITION BY id
ORDER BY "time" DESC)
可以在没有 WHERE 事件 IS NOT NULL 的情况下使用 LAST_VALUE(或 LEAD)加上 IGNORE NULLS 选项进行重写,但这将导致解释中的两个 STAT FUNCTION 步骤。
我有一个如下所示的 table:我已经按时间、ID 订购了 table。我想为每个 ID 找到最后一个事件 44 并获取它下面的值。
Table:
ID Time Event
A 00:10:00 11
A 00:10:11 44
A 00:10:13 22
A 00:10:00 11
A 00:10:11 44
A 00:10:13 33
A 00:10:13 22
B 01:10:00 ?
B 01:10:11 44
B 01:10:13 11
B 01:10:00 44
B 01:10:11 ?
B 01:10:13 22
... ... ...
想要的结果:
ID Time Event
A 00:10:13 33
B 01:10:13 22
... ... ...
已经尝试了不同的方法,但仍在努力...任何帮助将不胜感激。非常感谢!
假设您有一列实际上表示您确实想要下一行的顺序 和 ,这是一种方法:
select t.*
from (select t.*,
max(case when event = 44 then next_ordercol end) over (partition by id order by ordercol) as event44_next
from (select t.*,
lead(ordercol) over (partition by id order by ordercol) as next_ordercol
from t
) t
) t
where ordercol = event44_next;
我总是忘记 Teradata 没有实现 lead()
和 lag()
。我真的不明白。这是一种方法:
from (select t.*,
max(ordercol) over (partition by id order by ordercol rows between 1 following and 1 following) as next_ordercol
from t
) t
这将导致解释中的单个 STAT FUNCTION 步骤:
SELECT *
FROM tab
WHERE event IS NOT NULL
QUALIFY
-- previous event was 44
Min(event) -- LEAD
Over (PARTITION BY id
ORDER BY "time" DESC
ROWS BETWEEN 1 Following AND 1 Following) = 44
AND
-- only return rows when there was no previous 44 event
Count(CASE WHEN event = '44' THEN 1 end)
Over (PARTITION BY id
ORDER BY "time" DESC ROWS Unbounded Preceding) = 0
例如
ID TIME Event Lead Count
A 00:11:14 22 33 0
A 00:11:13 33 44 0
A 00:11:11 44 11 1
A 00:11:00 11 22 1
A 00:10:13 22 44 1
A 00:10:11 44 11 2
如果你 运行 TD16+ 终于支持 LAG/LEAD 而不是 MIN 重写:
Lead(event)
Over (PARTITION BY id
ORDER BY "time" DESC)
可以在没有 WHERE 事件 IS NOT NULL 的情况下使用 LAST_VALUE(或 LEAD)加上 IGNORE NULLS 选项进行重写,但这将导致解释中的两个 STAT FUNCTION 步骤。