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

相关推荐
lunz_fly199212 小时前
Oracle清理:如何安全删除trace, alert和archivelog文件?
oracle
薛定谔的算法14 小时前
phoneGPT:构建专业领域的检索增强型智能问答系统
前端·数据库·后端
Databend15 小时前
Databend 亮相 RustChinaConf 2025,分享基于 Rust 构建商业化数仓平台的探索
数据库
得物技术16 小时前
破解gh-ost变更导致MySQL表膨胀之谜|得物技术
数据库·后端·mysql
Raymond运维20 小时前
MariaDB源码编译安装(二)
运维·数据库·mariadb
沢田纲吉21 小时前
🗄️ MySQL 表操作全面指南
数据库·后端·mysql
RestCloud1 天前
SQL Server到Hive:批处理ETL性能提升30%的实战经验
数据库·api
RestCloud2 天前
为什么说零代码 ETL 是未来趋势?
数据库·api
ClouGence2 天前
CloudCanal + Paimon + SelectDB 从 0 到 1 构建实时湖仓
数据库
DemonAvenger2 天前
NoSQL与MySQL混合架构设计:从入门到实战的最佳实践
数据库·mysql·性能优化