最近在分区上使用 row_number() 的 SQL [英] SQL most recent using row_number() over partition

查看:25
本文介绍了最近在分区上使用 row_number() 的 SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一些网络点击数据,我只是在寻找带有访问过的 user_id(通过时间戳)的最新 page_name.使用下面的代码,user_id 重复,page_name 显示,按降序排序.但是,我只希望最近点击始终 = 1.完成后的查询将用作更大查询中的子查询.

I'm working with some web clicks data, and am just looking for the most recent page_name with the user_id visited (by a timestamp). Using the below code, the user_id is repeated and page_name with shown, with sorted descending. However, I would just like recent_click always = 1. The query when complete will be used as a subquery in a larger query.

这是我当前的代码:

 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

推荐答案

您应该能够将查询移动到子查询并添加 where 条件:

You should be able to move your query to a subquery and add where criteria:

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

这篇关于最近在分区上使用 row_number() 的 SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆