row_number() over(partition by xx order by xx desc)

一、目的

主要用于根据某个字段对数据分组去重

二、demo

1. 有数据表 duplicate_test 如下

2. 使用 name 作为 key 对数据分组,并增加一列标识序号 idx(根据 时间戳倒序标记序号)

sql 复制代码
select 
    name,
    row_number() over(partition by name order by timestamp desc) as idx,
    value,
    timestamp
from  duplicate_test 
where is_delete = 0

运行结果如下

3. 根据 name 作为 key,取每个分组里的第一条数据,从而实现 去重

sql 复制代码
select * from
(
    select 
        name,
        row_number() over(partition by name order by timestamp desc) as idx,
        value,
        timestamp
    from  duplicate_test 
    where is_delete = 0
) t where idx = 1

运行结果如下:

三、解释

以上sql 中只有一句核心代码:

row_number() over(partition by name order by timestamp desc) as idx

新增一列序号列 row_number(),根据 name 分组,每个分组里根据 timestamp 倒序排序,序号从 1 开始,起个别名 idx

四、sql 测试源码,数据库是 postgresql

sql 复制代码
-- 建表
create table duplicate_test (
    id bigserial NOT NULL,
    name varchar(50) NULL,
    value int2 NULL,
    is_delete int2 NOT NULL DEFAULT 0,
    timestamp timestamp(6)  NULL
);

-- 插入数据
insert into duplicate_test
(name, value, is_delete, timestamp)
VALUES('One', 11, 0, '2023-01-01 00:00:00');

insert into duplicate_test
(name, value, is_delete, timestamp)
VALUES('One', 22, 0, '2023-02-01 00:00:00');

insert into duplicate_test
(name, value, is_delete, timestamp)
VALUES('Two', 33, 0, '2023-01-01 00:00:00');

insert into duplicate_test
(name, value, is_delete, timestamp)
VALUES('Two', 44, 0, '2023-03-01 00:00:00');

insert into duplicate_test
(name, value, is_delete, timestamp)
VALUES('Two', 55, 0, '2023-05-01 00:00:00');

-- 分组排序
select 
    name,
    row_number() over(partition by name order by timestamp desc) as idx,
    value,
    timestamp
from  duplicate_test 
where is_delete = 0

-- 取每个组里的 时间戳最新的数据
select name, value from
(
    select 
        name,
        row_number() over(partition by name order by timestamp desc) as idx,
        value,
        timestamp
    from  duplicate_test 
    where is_delete = 0
) t where idx = 1

五、参考链接

Hive(十一)--数据去重及row_number()_hive row_number_zxfBdd的博客-CSDN博客

sql - 使用 ROW_NUMBER 和 PARTITION BY 获取第一行和最后一行 - SegmentFault 思否

相关推荐
ClouGence5 小时前
Oracle 数据同步为什么会出现数据不一致?长事务是常被忽略的原因
数据库·后端·oracle
飞将8 小时前
从零实现数据库(2)——HashIndex + IndexManager
数据库
Nturmoils1 天前
订单列表慢查询,先看 WHERE、ORDER BY 和 LIMIT
数据库
渣波1 天前
拒绝 SQL 焦虑!手把手带你用 NestJS + Prisma + DTO 写出“防弹”级后端代码
javascript·数据库·后端
倔强的石头_2 天前
KingbaseES 新版MySQL 兼容版体验:旧版迁移 + 功能实测
数据库
倔强的石头_5 天前
《Kingbase护城河》——数据库存储空间全景探测与精细化瘦身实战
数据库
冬奇Lab6 天前
每日一个开源项目(第134篇):Zvec - 阿里开源的嵌入式向量数据库,向量搜索界的 SQLite
数据库·人工智能·llm
ClouGence6 天前
Oracle CDC 架构优化:从主库直连到 DataGuard 备库同步
数据库·后端·oracle
无响应de神6 天前
三、用户与权限管理
数据库·mysql
麦聪聊数据7 天前
数据服务化时代:企业数据能力输出的核心路径
数据库