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

相关推荐
光影少年6 小时前
postgrsql和mysql区别?
数据库·mysql·postgresql
Hello.Reader6 小时前
Flink SQL Window Top-N窗口榜单的正确打开方式
数据库·sql·flink
wsx_iot6 小时前
MySQL 的 MVCC(多版本并发控制)详解
数据库·mysql
Shingmc37 小时前
MySQL表的增删改查
数据库·mysql
敲上瘾7 小时前
MySQL主从集群解析:从原理到Docker实战部署
android·数据库·分布式·mysql·docker·数据库架构
电子_咸鱼7 小时前
【QT——信号和槽(1)】
linux·c语言·开发语言·数据库·c++·git·qt
pandarking7 小时前
[CTF]攻防世界:web-unfinish(sql二次注入)
前端·数据库·sql·web安全·ctf
程序猿20237 小时前
MySQL索引性能分析
数据库·mysql
TDengine (老段)7 小时前
TDengine 新性能基准测试工具 taosgen
大数据·数据库·物联网·测试工具·时序数据库·tdengine·涛思数据
波波仔867 小时前
行存储与列存储的区别
数据库·clickhouse·行存储·列储存