日期:2014-05-16  浏览次数:20567 次

学习Oracle的分组函数和子查询

一,分组函数:

分组函数是作用于一组数据,并对一组数据返回一个值。

例如 AVG,COUNT,MAX,MIN,SUM,STDDEV这些都是常见的分组函数。

?

count(exp)? 返回exp不为空的记录总数。

count(distinct exp) 返回exp非空且不重复的记录总数。

?

在组函数中使用NVL函数:

NVL函数使分组函数无法忽略空值。

?

例如:

SELECT AVG(NVL(commeseon_con,0))? FROM? employess;

?

分组数据:group by 子句

使用group by 子句可以使表中数据分为若干组。

?

(1).在select 列表中所有未包含在组函数中的列都应该包含在group by 子句中,例如:

select department_id ,department_name,avg(salary)

from employees

group by department_id,department_name;

?

(2).包含在group by子句中的列不比包含在select列表中,例如:

select?? avg(salary)

from employees

group by department_id

?

(3)不能再where子句中使用组函数,可以在having子句中使用子函数

?

?

过滤分组:having子句

使用having分组

1,行以被分组

2,使用了组函数

3,满足having子句条件的分组呗显示出来

?

例如:

select department_id,max(salary)

from employees

group by department_id

having? max(salarg)>10000;

?

嵌套组函数:

?

举个例子吧:

select max(avg(salarg)) from employees? group by department_id;

?

?

二,子查询

子查询在主查询之前一次执行完成,子查询的结果被主查询使用。

例如:

select last_name

from employees

where salary>

?????? (select salary

??????? from employees

???????? where last_name='aNc'

???????? );

注意:

1,子查询要包含在括号内

2,将子查询放在比较条件的右侧

3,单行操作符对应单行子查询,多行操作符对应多行子查询。

?

单行子查询:

1,只返回一行

2,使用单行操作符 :=????,? <? , <= , > , >= , <>

?

例如:

select last_name,job_id,salary

from employees

where job_id =

??????????????????????????? (select job_id

????????????????????????????? from employees

???????????????????????????? where employee_id = 111)

and salary >

???????????????????????? (select salary

????????????????????????? from employees

????????????????????????? where employee_id = 121) ;

在子查询中也可以使用组函数,例如:

select last_name,job_id,salary

from employees

where? salary=

????????????????????????? (select min(salary)

??????????????????????????? from employees

??????????????????????????? );

?

子查询中的having子句

1,首先执行子查询

2,向主查询的having子句返回结果

例如:

select department_id,min(salary)

from employees

group by department_id

having min(salary) >

????????????????????????????? (select salary

?????????????????????????????? from employees

??????????????????????????????? where department_id =33);

?

非法使用子查询

例如:

select department_id,min(salary)

from employees

group by department_id

having min(salary) =

????????????????????????????? (select salary

?????????????????????????????? from employees

??????????????????????????????group by department_id); //子查询返回多个值

子查询如果为空值,就不返回任何值。

?

?

?

?

多行子查询:

1,返回多行

2,使用多行比较操作符? 例如:in?? all?? any

?

?

在多行子查询中用any

select employees_id,last_name,job_id,salary

from employees

where salary <? any

?????????????????????????????????????? (select salary

???????????????????????????????????????? from employees

???????????????????????????????????????? where job_id='sadsa')

and? job_id<>'sadsa';

?

在多行子查询中用all

select employees_id,last_name,job_id,salary

from employees

where salary <? all

?????????????????????????????????????? (select salary

???????????????????????????????????????? from employees

???????????????????????????????????????? whe