最近工作需要将oracle的存储过程转化为hive的sql脚本。遇到很多不一样的地方,例如oracle连接中有(+)号的用法。
借鉴这篇文章,但是这个排版比较烂。。。
oracle (+)的,Oracle中(+)的作用_大雪菜的博客-CSDN博客
先建表和插入数据
--生成部门表
CREATE TABLE Test_Departments
(
depID NUMBER(38,0),
depName VARCHAR2(20),
delFlag NUMBER(1,0)
);
--生成员工表
CREATE TABLE Test_Employees
(
empID NUMBER(38,0),
empName VARCHAR2(20),
depID NUMBER(38,0),
delFlag NUMBER(1,0)
);
--插入测试数据
INSERT INTO Test_Departments VALUES(1,'FI',0);
INSERT INTO Test_Departments VALUES(2,'MA',0);
INSERT INTO Test_Departments VALUES(3,'HR',1);
INSERT INTO Test_Departments VALUES(4,'IT',0);
INSERT INTO Test_Employees VALUES(1,'wbq',1,0);
INSERT INTO Test_Employees VALUES(2,'czh',2,0);
INSERT INTO Test_Employees VALUES(3,'chh',1,0);
INSERT INTO Test_Employees VALUES(4,'wal',2,0);
INSERT INTO Test_Employees VALUES(5,'ddd',3,0);
首先说明(+)代表什么? 代表这一侧的数据可以为空! a.id=b.id(+) 代表b表和a表关联的时候以a表作为主表。
测试1 这个是最常用的做法 以a为主表
SELECT A.*, B.*
FROM Test_Departments A
LEFT JOIN Test_Employees B ON A.depID = B.depID等价于
SELECT A.*, B.*
FROM Test_Departments A, Test_Employees B
WHERE A.depID = B.depID(+)
测试2 还是以a做主表,仍然保留a的所有数据
SELECT A.*, B.*
FROM Test_Departments A
LEFT JOIN Test_Employees B ON A.depID = B.depID
AND B.depID = 3等价于
SELECT A.*, B.*
FROM Test_Departments A, Test_Employees B
WHERE A.depID = B.depID(+)
AND (B.depID(+) = 3)
这里涉及到一个 left join 后面的过滤条件是on和where 的问题
select A.*,B.* from A left join B on A.id=B.id and B.id=3
返回结果:显示A表所有数据,B.id=3的数据,B.id不等于3的默认为空
select A.*,B.* from A,B where A.id=B.id(+) and B.id=3
返回结果:仅仅显示B.id=3的一条数据
测试3 保留a为主表,但是只显示过滤条件的数据
SELECT A.*, B.*
FROM Test_Departments A
LEFT JOIN Test_Employees B ON A.depID = B.depID
WHERE B.depID = 3等价于
SELECT A.*, B.*
FROM Test_Departments A, Test_Employees B
WHERE A.depID = B.depID(+)
AND (B.depID = 3)