oracle取出每组中的第一条记录

使用窗口函数ROW_NUMBER()配合PARTITION BY和ORDER BY,从table1中按type分组并按time排序,选取每组的第一条记录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SELECT * FROM(
SELECT t.type , t.money ,ROW_NUMBER() OVER(PARTITION BY t.type ORDER BY t.time) AS temp
FROM table1 t
)
WHERE temp =1;


这里涉及到的over()是oracle的分析函数
 
参考sql reference文档:
 
Analytic functions compute an aggregate value based on a group of rows. They differ from aggregate functions in that they return multiple rows for each group.
 
Analytic functions are the last set of operations performed in a query except for the final ORDER BY clause. All joins and all WHERE , GROUP BY , and HAVING clauses are completed before the analytic functions are processed. Therefore, analytic functions can appear only in the select list or ORDER BY clause.
 
语法结构:
 
analytic_function ([ arguments ]) OVER
 (analytic_clause)
其中analytic_clause结构包括:
 
[ query_partition_clause ]
[ order_by_clause [ windowing_clause ] ]
 
也就是:函数名( [ 参数 ] ) over( [ 分区子句 ] [ 排序子句 [ 滑动窗口子句 ] ])
 
     这里PARTITION BY 引导的分区子句类似于聚组函数中的group by,排序子句可看成是select语句中的order by.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值