是否可以在Teradata中“嵌套”限定语句?
我有一些看起来像这样的数据:
event_id = 1:
user_id action_timestamp
971,134,265 17mar2010 20:16:56
739,071,748 17mar2010 22:19:59
919,853,934 18mar2010 15:47:49
919,853,934 18mar2010 15:55:21
919,853,934 18mar2010 16:01:20
919,853,934 18mar2010 16:01:48
919,853,934 18mar2010 16:04:52
472,665,603 20mar2010 18:23:58
472,665,603 20mar2010 18:24:07
472,665,603 20mar2010 18:24:26
....
event_id = 2:
971,134,265 17mar2069 20:16:56
739,071,748 17mar2069 22:19:59
919,853,934 18mar2069 15:47:49
919,853,934 18mar2069 15:55:21
919,853,934 18mar2069 16:01:20
919,853,934 18mar2069 16:01:48
919,853,934 18mar2069 16:04:52
472,665,603 20mar2069 18:23:58
472,665,603 20mar2069 18:24:07
472,665,603 20mar2069 18:24:26
对于用户919,853,934,我想抓住“18mar2010 16:04:52”动作(第一组事件中的最后一个)。
我试过这个,但没有抓住正确的日期:
SELECT action_timestamp
,user_id
,event_id
FROM table
WHERE ...
QUALIFY (
MAX(action_timestampt) OVER (PARTITION BY user_id, event_id) = action_timestamp
AND MIN(action_timestamp) OVER (PARTITION BY user_id) = action_timestamp
)
这实际上是有意义的,因为MAX和MIN适用于整个数据,而不是顺序。
我还尝试了两个单独的限定语句来使MIN()部分应用于MAX()部分创建的数据子集,但是这些错误。
答案 0 :(得分:1)
这似乎达到了我想要的目的:
SELECT *
FROM
(SELECT *
FROM table
WHERE ...
QUALIFY (MAX(action_date) OVER (PARTITION BY user_id, event_id) = action_date)
) AS a
QUALIFY (
MIN(a.action_date) OVER (PARTITION BY a.user_id) = a.action_date
)
答案 1 :(得分:0)
此查询如何失败?
当然,您可以在QUALIFY中使用多个条件,您的查询在语法上是正确的。
但它的逻辑不会为给定的数据返回一行: - )
你可能需要重写它,也许
QUALIFY (
RANK() OVER (PARTITION BY user_id ORDER BY event_id) = 1
)
答案 2 :(得分:0)
只需按claue添加订单,然后检查它是否会获取第一个群集的最后一个值。
QUALIFY (
RANK() OVER (PARTITION BY user_id ORDER BY event_id order by action_timestamp desc ) = 1
)
注意:如果您只想获得18mar2010 16:04:52
used_id =1
值,那么您的工作是否有效