oracle总结2

1.分组统计

组函数忽略列中的空值

group by select_list
having ...  ----having用来对分组设置过滤条件

distinct  唯一显示,表示值不重复
---count() 求行数
---avg()   求平均值
---sum()  求和
---max()  求最大值
---min()  求最小值


---count()
select count(*) counts from employees;

---avg()
select round(avg(salary),2) avg_salary  from employees;

---sum()
select sum(salary) sum_salary  from employees;

---max()
select max(salary) from employees;
   
---min()   
select min(salary) from employees;

---group by ,having
select nvl(department_id,0) department_id,sum(salary) sum_salary
   from employees group by nvl(department_id,0) having sum(salary) > 100000;
   
---distinct
 select distinct max_salary from jobs
 order by max_salary desc;


#组函数忽略列中的空值

select round(avg(COMMISSION_PCT),2) avg_pct from employees;
 
select round(avg(nvl(COMMISSION_PCT,0)),2) avg_pct from employees;
 
select round(avg(COMMISSION_PCT),2) avg_pct from employees where COMMISSION_PCT is not null;


2.子查询

单行子查询:只返回一行
多行子查询:返回多行数据

---单行子查询
查询雇员编号为141的last_name,job_id

 
 select job_id,last_name from employees where job_id=(
  select job_id from employees where employee_id =141);

---多行子查询
in/any/all


in:等于列表中任何成员
any:比较子查询返回的每个值
all:比较子查询返回的全部值

//每个部门最少薪水的雇员编号和雇员

select  employee_id,last_name  from employees
where salary in
(select min(salary) from employees group by department_id);

---any
select  employee_id,last_name,salary  from employees where salary < any(

select min(salary) from employees where job_id='IT_PROG');


3.rownum(伪列)

rownum并非在表中存在的真正的列,操作rownum只能是select,rownum用来标识结果集中每一条记录的顺序,对于结果集中第一条记录,其rownum为1.

---显示10行记录
select employee_id,last_name,rownum from employees where rownum <=10;

































 








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值