oracle_常用分組函數,oracle分組函數

來源:互聯網
上載者:User

oracle_常用分組函數,oracle分組函數


oracle_常用分組函數
①分組函數
1.max(column):求最大值,對資料類型沒有要求,任意資料類型都可以
2.min(column):求最小值,對資料類型沒有要求,任意資料類型都可以
3.avg(column):返回column不為null的平均值,對資料類型有要求,只能針對number類型(數字類型)
4.sum(column):返回column不為null的總和,對資料類型有要求,只能針對number類型(數字類型)
5.count(column):返回column不為null的記錄數,對資料類型沒有要求,任意資料類型都可以

註:組函數忽略空值
COUNT(DISTINCT expr)返回expr非空且不重複的記錄總數

②分組
分組資料:  GROUP BY 子句文法
可以使用GROUP BY子句將表中的資料分成若干組
在SELECT 列表中所有未包含在組函數中的列都應該包含在 GROUP BY 子句中。
包含在 GROUP BY 子句中的列不必包含在SELECT 列表中
所有包含於SELECT 列表中,而未包含於組函數中的列都必須包含於 GROUP BY 子句中。

不能在 WHERE 子句中使用組函數。
可以在 HAVING 子句中使用組函數

③過濾分組: HAVING 子句
使用 HAVING 過濾分組:
1.行已經被分組。
2.使用了組函數。
3.滿足HAVING 子句中條件的分組將被顯示。

④組函數嵌套
max(avg(salary))




oracle中的分組函數有什,具體怎使用?

常用的有:
COUNT() 返回查尋的行數
例如:select count(*) from table;
MAX() 返回運算式的最大值
例如:select a, max(b) from table group by a;
MIN() 返回運算式的最小值
例如:select a, min(b) from table group by a;
SUM() 返回運算式的總合
例如:select a, sum(b) from table group by a;
AVG() 返回運算式的平均值
例如:select a, avg(b) from table group by a;
此外還有分析函數over,是用來處理複雜sql的,這個涉及到的東西就有很多了。一兩句話說不清楚,如果有關於這方面的問題,可以發求助或追問。
 
oracle 分組函數

第二種理解不對,第二種是對a和b同時進行分組,你看一下下面的例子你就知道:
create table test
(a int,
b int,
c int);

insert into test values(1,2,1)
insert into test values(1,2,2);
insert into test values(1,3,1);
insert into test values(1,3,2);
insert into test values(2,2,1);
insert into test values(2,2,2);
insert into test values(3,2,3);
insert into test values(4,2,4);

select a ,count(*) "統計" from test t
group by t.a ;

select a ,b,count(*) "統計" from test t
group by t.a ,t.b;



 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.