160804、oracle查询:取出每组中的第一条记录
oracle查詢:取出每組中的第一條記錄
按type字段分組,code排序,取出每組中的第一條記錄
?
方法一:
?
select type,min(code) from group_info?
group by type;
?
注意:select 后面的列要在group by 子句中,或是用聚合函數(shù)包含,否則會(huì)有語法錯(cuò)誤。
?
方法二:
?
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的分析函數(shù)
?
參考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.
?
語法結(jié)構(gòu):
?
analytic_function ([ arguments ]) OVER(analytic_clause)其中analytic_clause結(jié)構(gòu)包括:
?
[ query_partition_clause ] [ order_by_clause [ windowing_clause ] ]?
也就是:函數(shù)名( [ 參數(shù) ] ) over( [ 分區(qū)子句 ]? [ 排序子句 [ 滑動(dòng)窗口子句 ] ])
?
???? 這里PARTITION BY 引導(dǎo)的分區(qū)子句類似于聚組函數(shù)中的group by,排序子句可看成是select語句中的order by.
轉(zhuǎn)載于:https://www.cnblogs.com/zrbfree/p/5752399.html
總結(jié)
以上是生活随笔為你收集整理的160804、oracle查询:取出每组中的第一条记录的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 给服务器端下拉asp:DropDownL
- 下一篇: Angular之filter学习