-
-使用
case表达式
- -在 SQL语句中有两种方法被用来实现条件
- -处理(IF - THEN - ELSE 逻辑) : CASE表达式和DECODE函数。
- -注意: CASE表达式和ANSI SQL一致。
- -DECODE函数是特有的Oracle语法
- - case表达式有两种形式: simple case和searched case
- - 1) simple case:简单 case
- -语法:
case 列名
when 值 1 then 返回值 1
[ when 值 2 then 返回值 2
when 值n then 返回值n
else 返回值] - -返回值用“”
end
- -将某个列值和值 1比较,如果相等则返回“返回值 1”;
- -如果不等则将列值和值 2比较,如果相等则返回“返回值 2”;
- -以此类推。如果列值和值 1、值 2、。。。值n都不等,则
- -返回 else后的值。
- -要求所有的返回值必须是同一个数据类型。
select last_name,job_id,salary,
case job_id
when 'IT_PROG' then 1. 10 *salary
when 'ST_CLERK' then 1. 15 *salary
when 'SA_REP' then 1. 20 *salary
else salary end "revised_salary"
from employees;
- - 1)searched case:搜索式 case
- -语法:
case
when 列名 =值 1 then 返回值 1
[ when 列名 =值 2 then 返回值 2
when 列名 =值n then 返回值n
else 返回值]
end
- -以搜索式 case改写上例
select last_name,job_id,salary,
case
when job_id = 'IT_PROG' then 1. 10 *salary
when job_id = 'ST_CLERK' then 1. 15 *salary
when job_id = 'SA_REP' then 1. 20 *salary
else salary end "revised_salary"
from employees;
- -decode函数实现和 case表达式一样的功能
select last_name,job_id,salary,
decode(job_id
, 'IT_PROG' , 1. 10 *salary
, 'ST_CLERK' , 1. 15 *salary
, 'SA_REP' , 1. 20 *salary
, salary ) "revised_salary"
from employees;
- -使用组函数(聚集函数)
- -组函数对一组一行进行操作并返回一个值。
- -所有的组函数默认情况下会忽略 null值
- - 5个组函数:
- - avg、 max、 min、 sum、 count
- -查询所有销售代表的平均月薪、最低月薪、最高月薪和月薪总和
select avg(salary), min(salary),
max(salary), sum(salary)
from employees
where job_id like '%REP%';
- -注意:上面的查询对返回的结果集中的行没有明确地进行分组,
- -那么结果集中的所有行自动形成一个组。然后对该组行应用
- -组函数进行计算,最终返回的结果集中只有一行(因为只有一组)
- -对日期和字符串都可以求最大和最小
- -查询雇员的最早和最晚的入职日期
select min(hire_date), max(hire_date)
from employees;
select min(last_name), max(last_name)
from employees;
- - count函数:返回表中满足条件的行的数量
- -常见 3种写法:
- - COUNT( *)
- - COUNT(expr)
- - COUNT( DISTINCT expr)
- - 1、 COUNT( *):返回表中满足 where条件的行的数量
- -查询部门 50的雇员的数量
select count( *)
from employees
where department_id = 50;
- -查询雇员的总数量
select count( *)
from employees;
- - 2、 COUNT(列):返回表中列值非空的行的数量
- -查询拿佣金的雇员的数量
select count(commission_pct)
from employees;
- - 3、 COUNT( distinct 列):返回表中列值非空的并且列值不重复的行的数量
- -查询所有雇员分布的部门的数量
select count( distinct department_id)
from employees;
- -组函数和
- -在 SQL语句中有两种方法被用来实现条件
- -处理(IF - THEN - ELSE 逻辑) : CASE表达式和DECODE函数。
- -注意: CASE表达式和ANSI SQL一致。
- -DECODE函数是特有的Oracle语法
- - case表达式有两种形式: simple case和searched case
- - 1) simple case:简单 case
- -语法:
case 列名
when 值 1 then 返回值 1
[ when 值 2 then 返回值 2
when 值n then 返回值n
else 返回值] - -返回值用“”
end
- -将某个列值和值 1比较,如果相等则返回“返回值 1”;
- -如果不等则将列值和值 2比较,如果相等则返回“返回值 2”;
- -以此类推。如果列值和值 1、值 2、。。。值n都不等,则
- -返回 else后的值。
- -要求所有的返回值必须是同一个数据类型。
select last_name,job_id,salary,
case job_id
when 'IT_PROG' then 1. 10 *salary
when 'ST_CLERK' then 1. 15 *salary
when 'SA_REP' then 1. 20 *salary
else salary end "revised_salary"
from employees;
- - 1)searched case:搜索式 case
- -语法:
case
when 列名 =值 1 then 返回值 1
[ when 列名 =值 2 then 返回值 2
when 列名 =值n then 返回值n
else 返回值]
end
- -以搜索式 case改写上例
select last_name,job_id,salary,
case
when job_id = 'IT_PROG' then 1. 10 *salary
when job_id = 'ST_CLERK' then 1. 15 *salary
when job_id = 'SA_REP' then 1. 20 *salary
else salary end "revised_salary"
from employees;
- -decode函数实现和 case表达式一样的功能
select last_name,job_id,salary,
decode(job_id
, 'IT_PROG' , 1. 10 *salary
, 'ST_CLERK' , 1. 15 *salary
, 'SA_REP' , 1. 20 *salary
, salary ) "revised_salary"
from employees;
- -使用组函数(聚集函数)
- -组函数对一组一行进行操作并返回一个值。
- -所有的组函数默认情况下会忽略 null值
- - 5个组函数:
- - avg、 max、 min、 sum、 count
- -查询所有销售代表的平均月薪、最低月薪、最高月薪和月薪总和
select avg(salary), min(salary),
max(salary), sum(salary)
from employees
where job_id like '%REP%';
- -注意:上面的查询对返回的结果集中的行没有明确地进行分组,
- -那么结果集中的所有行自动形成一个组。然后对该组行应用
- -组函数进行计算,最终返回的结果集中只有一行(因为只有一组)
- -对日期和字符串都可以求最大和最小
- -查询雇员的最早和最晚的入职日期
select min(hire_date), max(hire_date)
from employees;
select min(last_name), max(last_name)
from employees;
- - count函数:返回表中满足条件的行的数量
- -常见 3种写法:
- - COUNT( *)
- - COUNT(expr)
- - COUNT( DISTINCT expr)
- - 1、 COUNT( *):返回表中满足 where条件的行的数量
- -查询部门 50的雇员的数量
select count( *)
from employees
where department_id = 50;
- -查询雇员的总数量
select count( *)
from employees;
- - 2、 COUNT(列):返回表中列值非空的行的数量
- -查询拿佣金的雇员的数量
select count(commission_pct)
from employees;
- - 3、 COUNT( distinct 列):返回表中列值非空的并且列值不重复的行的数量
- -查询所有雇员分布的部门的数量
select count( distinct department_id)
from employees;
- -组函数和