我正在使用一些网络点击数据,我只是在访问user_id(通过时间戳)查找最新的page_name。使用下面的代码,重复user_id并显示page_name,并按降序排序。但是,我只想使用recent_click always = 1.完成时的查询将用作较大查询中的子查询。
这是我目前的代码:
SELECT user_id,
page_name,
row_number() over(partition by session_id order by ts desc) as recent_click
from clicks_data;
user_id | page_name | recent_click
--------+-------------+--------------
0001 | login | 1
0001 | login | 2
0002 | home | 1
答案 0 :(得分:17)
您应该能够将查询移至子查询并添加where
条件:
SELECT user_id, page_name, recent_click
FROM (
SELECT user_id,
page_name,
row_number() over (partition by session_id order by ts desc) as recent_click
from clicks_data
) T
WHERE recent_click = 1
答案 1 :(得分:2)
您应该将row_number()
函数移动到子查询中,然后在外部查询中对其进行过滤
像这样:
SELECT * FROM (
SELECT
[user_id]
,[page_name]
,ROW_NUMBER() OVER (PARTITION BY [session_id]
ORDER BY [ts] DESC) AS [recent_click]
FROM [clicks_data]
)x
WHERE [recent_click] = 1