《oracle查询语句2》

单行函数

SQL> select upper(first_name), lower(last_name), length(last_name) from employees;

SQL> select (sysdate-hire_date)/7 from employees;

SQL> select trunc((sysdate-hire_date)/30, 0) from employees;

SQL> select trunc(months_between(sysdate,hire_date), 0) from employees;//解决累积误差

SQL> select sysdate+3650 from dual;

SQL> select add_months(sysdate, 120) from dual;//解决累积误差

SQL> select next_day(‘2015-09-01‘, ‘friday‘) from dual; //下一个周五

SQL> select next_day(‘2015-10-01‘, 6) from dual;

SQL> select last_day(sysdate) from dual;

SQL> select round(to_date(‘2015-10-10‘,‘yyyy-mm-dd‘), ‘MONTH‘) from dual;

SQL> select round(to_date(‘2015-10-16‘,‘yyyy-mm-dd‘), ‘MONTH‘) from dual;

SQL> select round(to_date(‘2015-10-10‘,‘yyyy-mm-dd‘), ‘YEAR‘) from dual;

SQL> select round(sysdate, ‘DAY‘) from dual;

练习:

找出各月最后三天内受雇的所有雇员

extract(month from hire_date+4) != extract(month from hire_date)

找出早于25年之前受雇的雇员

months_between(sysdate, hire_date)/300>=25

显示正好为6个字符的雇员姓名

length(last_name)=6

显示所有雇员的姓名的前三个字符

substr(last_name, 1, 3)

显示所有雇员的姓名,用a替换所有‘A‘

replace(last_name, ‘A‘, ‘a‘)

类型转换和其他函数

SQL> select to_char(salary, ‘$999,999.00‘) from employees;

SQL> select last_name, to_char(hire_date, ‘dd-Mon-RR‘) from employees;

SQL> select to_char(sysdate, ‘yyyy-mm-dd hh24:mi:ss‘) from dual;

SQL> select to_char(sysdate, ‘yyyy-mm-dd hh:mi:ss AM‘) from dual;

SQL> select last_name from employees where hire_date=to_date(‘2006-05-23‘, ‘yyyy-mm-dd‘);

SQL> select to_number(‘$123,456.78‘, ‘$999,999.00‘) from dual;

练习:

查询2006年入职员工:

select last_name

from employees

where hire_date between to_date(‘2006-01-01‘, ‘yyyy-mm-dd‘)

and  to_date(‘2006-12-31‘, ‘yyyy-mm-dd‘);

select last_name

from employees

where to_char(hire_date, ‘yyyy‘)=‘2006‘;

select last_name

from employees

where extract(year from hire_date)=2006;

--不推荐

select last_name

from employees

where hire_date like ‘2006%‘;

查询历年9月份入职的员工:

select last_name

from employees

where to_char(hire_date, ‘mm‘)=‘09‘;

select last_name

from employees

where extract(month from hire_date)=9;

其他函数:

nvl:

nvl(val1, val2)

if val1 is not null

then

return val1;

else

return val2;

SQL> select last_name, salary*12*(1+nvl(commission_pct, 0)) total_salary from employees;

练习:

显示所有员工部门编号,没有部门的显示“未分配部门”

case和decode:

IT_PROG +1000

SA_REP +1500

ST_CLERK +2000

其他人工资不变

select salary+1000 from employees where job_id=‘IT_PROG‘;

select last_name, job_id, salary,

case job_id

when ‘IT_PROG‘ then salary+1000

when ‘SA_REP‘ then salary+1500

when ‘ST_CLERK‘ then salary+2000

else salary

end new_salary

from employees;

select last_name, job_id, salary,

decode( job_id,

‘IT_PROG‘, salary+1000,

‘SA_REP‘,  salary+1500,

‘ST_CLERK‘, salary+2000,

salary) new_salary

from employees;

练习:

按照员工工资,对员工分级显示:

A 20001-25000

B 15001-20000

C 10001-15000

D 5001-10000

E 0-5000

 

 

分组函数

只有  count(*):对行做统计,对空值保留做统计,其他都是将空值去掉后做统计

Avg:对非空值取平均值

Nvl:将空值赋予值为0,在做统计

Group by分组做统计时,不会将空值舍去

Select 中只会出现分组列,分组函数

SQL> select count(*), sum(salary), avg(salary), minsalary), max(salary) from employees;

SQL> create table t1(x int);

SQL> insert into t1 values (null);

SQL> insert into t1 values (1);

SQL> commit;

SQL> select count(*) from t1;

SQL> select count(x) from t1;

SQL> select max(x) from t1;

SQL> select min(x) from t1;

SQL> select sum(x) from t1;

SQL> select avg(x) from t1;

SQL> select avg(salary), avg(nvl(commission_pct, 0)) from employees;     对空值赋值为0做统计

SQL> select count(distinct department_id) from employees; 去除重复值做统计

Group by分组:

SQL> select department_id, avg(salary) from employees group by department_id;

多列分组:

SQL> select department_id, job_id, max(salary) from employees group by department_id, job_id;

SQL> select department_id, job_id, max(salary), last_name from employees group by department_id, job_id; 错误语法

练习:

公司中不同职位的数量

SQL> select count(distinct job_id) from employee

计算每个部门的人数

SQL> select  department_id, count(employee_id) from employees group by department_id;

=SQL> select  department_id, count(last_name) from employees group by department_id;

按年份分组,求员工的工资总和

SQL> select extract(year from hire_date), sum(salary) from employees group by extract(year from hire_date);  //将年份抽取出来

Having语句:相当于group by之后的where

SQL> select department_id, avg(salary) from employees where avg(salary)>=5000 group by department_id; 错误语句

SQL> select department_id, avg(salary) from employees group by department_id having avg(salary)>=5000;

练习:

按部门求出所有有部门的普通员工的平均工资,部门平均工资少于5000的不显示,最终结果按平均工资的降序排列。

select department_id, avg(salary) avg_sal

from employees

where job_id not like ‘%\_MGR‘ escape ‘\‘ and department_id is not null

group by department_id

having avg(salary)>=5000

order by avg_sal desc;

多表连接

emp: dept:

empno ename deptno deptno dname

100 abc 10 10 sales

101 def 10 20 market

102 xyz 20 30 it

103 opq null

for emp in 100 .. 103

for dept in 10 .. 30

emp.deptno=dept.deptno

100         abc         10              10          sales

101         def         10              10          sales

102         xyz         20              20          market

订单表:

CustID  StoreID     ProdID  ChannelID

100 S100        P100    C100

客户表:

CustID  name  creditlevel

100         abc

地址表:

CustID  adress

100         bj

100         tj

获取如下信息,准备工作:

employees:

员工总数:107

SQL> select count(*) from employees;

有部门的员工数:106

SQL> select count(*) from employees where department_id is not null;

SQL> select count(department_id) from employees;

没有部门的员工数:1

SQL> select count(*) from employees where department_id is null;

departments:

部门总数:27

SQL> select count(*) from departments;

有员工的部门数:11

SQL> select count(distinct department_id) from employees;

没有员工的部门数:16

SQL> select count(*) from departments where department_id not in (select department_id from employees where department_id is not null);

for dept in 1..27

for emp in 1..107

dept.deptid不在emp表中出现

where e.department_id(+)=d.department_id

select count(*)

from employees e, departments d

and e.employee_id is null;

select count(*)

from departments d

where not exists

(select 1 from employees where department_id=d.department_id);

select (select count(*) from departments)-(select count(distinct department_id) from employees) from dual;

内连接:106(106, 11)

select e.last_name, d.department_name

from employees e, departments d               //对表进行重命名

where e.department_id=d.department_id;  //

select e.last_name, d.department_name

from employees e join departments d on e.department_id=d.department_id;

左外连接:107(106+1) //将e表的不符合的也添加进来,就在d的后面+“(+)”

select e.last_name, d.department_name

from employees e, departments d

where e.department_id=d.department_id(+);

select e.last_name, d.department_name

from departments d, employees e

where e.department_id=d.department_id(+);

Sql99标准写法

select e.last_name, d.department_name

from employees e left outer join departments d

on e.department_id=d.department_id;

右外连接:122(106+16)

select e.last_name, d.department_name

from employees e, departments d

where e.department_id(+)=d.department_id;

select e.last_name, d.department_name

from employees e right outer join departments d

on e.department_id=d.department_id;

完全外连接:123(106+1+16)

select e.last_name, d.department_name

from employees e full outer join departments d

on e.department_id=d.department_id;

多表连接的扩展:

n张表连接:

select e.last_name, d.department_name, l.city

from employees e, departments d, locations l

where e.department_id=d.department_id

and d.location_id=l.location_id;

select e.last_name, d.department_name, l.city

from employees e join departments d on e.department_id=d.department_id

join locations l on d.location_id=l.location_id;

select e.last_name, d.department_name, l.city

from employees e, departments d, locations l

where e.department_id=d.department_id(+)

and d.location_id=l.location_id(+);

select e.last_name, d.department_name, l.city

from employees e left outer join departments d on e.department_id=d.department_id

left outer join locations l on d.location_id=l.location_id;

时间: 2024-10-14 00:40:34

《oracle查询语句2》的相关文章

CI框架源码阅读笔记3 全局函数Common.php

从本篇开始,将深入CI框架的内部,一步步去探索这个框架的实现.结构和设计. Common.php文件定义了一系列的全局函数(一般来说,全局函数具有最高的加载优先权,因此大多数的框架中BootStrap引导文件都会最先引入全局函数,以便于之后的处理工作). 打开Common.php中,第一行代码就非常诡异: if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 上一篇(CI框架源码阅读笔记2 一切的入口 index

IOS测试框架之:athrun的InstrumentDriver源码阅读笔记

athrun的InstrumentDriver源码阅读笔记 作者:唯一 athrun是淘宝的开源测试项目,InstrumentDriver是ios端的实现,之前在公司项目中用过这个框架,没有深入了解,现在回来记录下. 官方介绍:http://code.taobao.org/p/athrun/wiki/instrumentDriver/ 优点:这个框架是对UIAutomation的java实现,在代码提示.用例维护方面比UIAutomation强多了,借junit4的光,我们可以通过junit4的

Yii源码阅读笔记 - 日志组件

?使用 Yii框架为开发者提供两个静态方法进行日志记录: Yii::log($message, $level, $category);Yii::trace($message, $category); 两者的区别在于后者依赖于应用开启调试模式,即定义常量YII_DEBUG: defined('YII_DEBUG') or define('YII_DEBUG', true); Yii::log方法的调用需要指定message的level和category.category是格式为“xxx.yyy.z

源码阅读笔记 - 1 MSVC2015中的std::sort

大约寒假开始的时候我就已经把std::sort的源码阅读完毕并理解其中的做法了,到了寒假结尾,姑且把它写出来 这是我的第一篇源码阅读笔记,以后会发更多的,包括算法和库实现,源码会按照我自己的代码风格格式化,去掉或者展开用于条件编译或者debug检查的宏,依重要程度重新排序函数,但是不会改变命名方式(虽然MSVC的STL命名实在是我不能接受的那种),对于代码块的解释会在代码块前(上面)用注释标明. template<class _RanIt, class _Diff, class _Pr> in

CI框架源码阅读笔记5 基准测试 BenchMark.php

上一篇博客(CI框架源码阅读笔记4 引导文件CodeIgniter.php)中,我们已经看到:CI中核心流程的核心功能都是由不同的组件来完成的.这些组件类似于一个一个单独的模块,不同的模块完成不同的功能,各模块之间可以相互调用,共同构成了CI的核心骨架. 从本篇开始,将进一步去分析各组件的实现细节,深入CI核心的黑盒内部(研究之后,其实就应该是白盒了,仅仅对于应用来说,它应该算是黑盒),从而更好的去认识.把握这个框架. 按照惯例,在开始之前,我们贴上CI中不完全的核心组件图: 由于BenchMa

CI框架源码阅读笔记2 一切的入口 index.php

上一节(CI框架源码阅读笔记1 - 环境准备.基本术语和框架流程)中,我们提到了CI框架的基本流程,这里这次贴出流程图,以备参考: 作为CI框架的入口文件,源码阅读,自然由此开始.在源码阅读的过程中,我们并不会逐行进行解释,而只解释核心的功能和实现. 1.       设置应用程序环境 define('ENVIRONMENT', 'development'); 这里的development可以是任何你喜欢的环境名称(比如dev,再如test),相对应的,你要在下面的switch case代码块中

Apache Storm源码阅读笔记

欢迎转载,转载请注明出处. 楔子 自从建了Spark交流的QQ群之后,热情加入的同学不少,大家不仅对Spark很热衷对于Storm也是充满好奇.大家都提到一个问题就是有关storm内部实现机理的资料比较少,理解起来非常费劲. 尽管自己也陆续对storm的源码走读发表了一些博文,当时写的时候比较匆忙,有时候衔接的不是太好,此番做了一些整理,主要是针对TridentTopology部分,修改过的内容采用pdf格式发布,方便打印. 文章中有些内容的理解得益于徐明明和fxjwind两位的指点,非常感谢.

CI框架源码阅读笔记4 引导文件CodeIgniter.php

到了这里,终于进入CI框架的核心了.既然是"引导"文件,那么就是对用户的请求.参数等做相应的导向,让用户请求和数据流按照正确的线路各就各位.例如,用户的请求url: http://you.host.com/usr/reg 经过引导文件,实际上会交给Application中的UsrController控制器的reg方法去处理. 这之中,CodeIgniter.php做了哪些工作?我们一步步来看. 1.    导入预定义常量.框架环境初始化 之前的一篇博客(CI框架源码阅读笔记2 一切的入

jdk源码阅读笔记之java集合框架(二)(ArrayList)

关于ArrayList的分析,会从且仅从其添加(add)与删除(remove)方法入手. ArrayList类定义: p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Monaco } span.s1 { color: #931a68 } public class ArrayList<E> extends AbstractList<E> implements List<E> ArrayList基本属性: /** *

dubbo源码阅读笔记--服务调用时序

上接dubbo源码阅读笔记--暴露服务时序,继续梳理服务调用时序,下图右面红线流程. 整理了调用时序图 分为3步,connect,decode,invoke. 连接 AllChannelHandler.connected(Channel) line: 38 HeartbeatHandler.connected(Channel) line: 47 MultiMessageHandler(AbstractChannelHandlerDelegate).connected(Channel) line: