1,group by colum_name;
如:对ku_id,按ku_lid进行分组求和;
SQL> run
1 select sum(ku_id),ku_lid from wk_t_user
2* group by ku_lid
SUM(KU_ID) KU_LID
---------- --------------------
1 guest
13 audit
2 superman
12 info
11 admin
2,函数:sum(colum),avg(colum),max(colum),min(colum);
3,聚合函数count;在oracle环境中,在使用count时,缺省情况下count忽略空值,
即:count(non null values)或count(distinct expr);
如:SQL> select count(*) from wk_t_user;
COUNT(*)
----------
5
SQL> select count(ku_name) from wk_t_user;
COUNT(KU_NAME)
--------------
4
4,假定:avg(NVL(ku_id,0)),ku_id为空,则假定它为0,表示这一行有效;
SQL> select avg(nvl(ku_id,0)) from wk_t_user;
AVG(NVL(KU_ID,0))
-----------------
7.8
5,group by的使用方法;
SQL> select ku_lid ,sum(ku_id) from wk_t_user
2 group by ku_lid;
KU_LID SUM(KU_ID)
-------------------- ----------
guest 1
audit 13
superman 2
info 12
admin 11
SQL> select ku_lid,sum(ku_id) from wk_t_user;
select ku_lid,sum(ku_id) from wk_t_user
*
第 1 行出现错误:
ORA-00937: 不是单组分组函数
可以按多个字段分组:
SQL> select ku_id,ku_lid,sum(ku_id) from wk_t_user
2 group by ku_id,ku_lid;
KU_ID KU_LID SUM(KU_ID)
---------- -------------------- ----------
2 superman 2
12 info 12
1 guest 1
11 admin 11
13 audit 13
6,having与聚合函数一起使用,相当与select中的where;
SQL> select ku_id,sum(ku_id) from wk_t_user
2 group by ku_id
3 having sum(ku_id)>10;
KU_ID SUM(KU_ID)
---------- ----------
11 11
12 12
13 13
阅读(4494) | 评论(0) | 转发(0) |