oracle分组查询取第一条数据,160804、oracle查询:取出每组中的第一条记录

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

按type字段分组,code排序,取出每组中的第一条记录

方法一:

select type,min(code) from group_info

group by type;

注意:select 后面的列要在group by 子句中,或是用聚合函数包含,否则会有语法错误。

方法二:

SELECT * FROM(

SELECT z.type , z.code ,ROW_NUMBER()

OVER(PARTITION BY z.type ORDER BY z.code) AS code_id

FROM group_info z

)

WHERE code_id =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 on

你可能感兴趣的:(oracle分组查询取第一条数据,160804、oracle查询:取出每组中的第一条记录)