ORACLE多列中取出数据最大的一条

1.需求说明:

当查询出来的数据存在多条数据时,想按照一定条件排序取出其中一条数据。

2.使用函数:

row_number() over( partition by 分组字段 order by 排序字段 desc)

3.示例:

--根据table_a中的pk_house,pk_customer进行分组,然后根据table_b.billdate进行排序,取出最大billdate的一条数据

select *
from (select table_a.code,
table_a.name,
table_b.billdate,
row_number() over(partition by table_a.pk_house, table_a.pk_customer order by table_b.billdate desc) rn
from table_a
left join table_b
on table_b.pk_table_a = table_a.pk_table_a
where table_a.code = '11111')
where rn = 1

相关推荐
AAA修煤气灶刘哥7 小时前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
RestCloud11 小时前
揭秘 CDC 技术:让数据库同步快人一步
数据库·api
得物技术14 小时前
MySQL单表为何别超2000万行?揭秘B+树与16KB页的生死博弈|得物技术
数据库·后端·mysql
可涵不会debug18 小时前
【IoTDB】时序数据库选型指南:工业大数据场景下的技术突围
数据库·时序数据库
ByteBlossom18 小时前
MySQL 面试场景题之如何处理 BLOB 和CLOB 数据类型?
数据库·mysql·面试
麦兜*18 小时前
MongoDB Atlas 云数据库实战:从零搭建全球多节点集群
java·数据库·spring boot·mongodb·spring·spring cloud
Slaughter信仰18 小时前
深入理解Java虚拟机:JVM高级特性与最佳实践(第3版)第十章知识点问答(10题)
java·jvm·数据库
麦兜*18 小时前
MongoDB 在物联网(IoT)中的应用:海量时序数据处理方案
java·数据库·spring boot·物联网·mongodb·spring
努力也学不会java18 小时前
【设计模式】抽象工厂模式
java·设计模式·oracle·抽象工厂模式