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

相关推荐
张哈大1 小时前
【 Redis | 实战篇 秒杀实现 】
数据库·redis·缓存
weixin_472339461 小时前
Postgresql与openguass对比
数据库·postgresql
惊起白鸽4506 小时前
MySQL全量,增量备份与恢复
数据库·mysql
暮雨疏桐7 小时前
MySQL SQL Mode及其说明
数据库·sql·mysql·sql mode
Tangcan-7 小时前
【MySQL】数据库基础
数据库·mysql
蔡蓝7 小时前
Mysql的索引,慢查询和数据库表的设计以及乐观锁和悲观锁
数据库·mysql
jstart千语7 小时前
【Redis】分布式锁的实现
数据库·redis·分布式
一把年纪学编程9 小时前
【牛马技巧】word统计每一段的字数接近“字数统计”
前端·数据库·word
极小狐9 小时前
极狐GitLab 通用软件包存储库功能介绍
java·数据库·c#·gitlab·maven
钢铁男儿9 小时前
C# 方法(可选参数)
数据库·mysql·c#