0%

分组查询

常用分组函数

1
2
-- avg,sum,min,max,count,wm_concat
select avg(sal),sum(sal),min(sal),max(sal),count(sal) from emp;
1
select count(*),count(comm) from emp;

上面二个值不一样,是因为分组函数会自动忽略空值,会自动忽略空值。NVL函数可以使分组函数无法忽略空值,例如下面的sql:

1
select count(*),count(nvl(comm, 0)) from emp;

group by语句

1
2
3
select column, from table_name [where condition] [group by group_by_condition] [having group_condition] [order by column];
-- 平均工资
select deptno,avg(sal) from emp group by deptno;

错误写法:

1
2
select a,b,avg(x) from emp group by a;
-- b必须出现在group by 后面。
Read more »