楼主: dingjun123

[精华] 【PDF已传上】准备写一个oracle高级sql的参考资料

[复制链接]
论坛徽章:
520
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
201#
发表于 2010-1-22 22:26 | 只看该作者
原帖由 dingjun123 于 2010-1-22 09:21 发表
对于MINUS运算,在WHERE子句中所有的列都必须在SELECT子句中。(10个可以) 这个是错了,没有这个限制。
缺省排序好像除了union all,其它都是按第1列升序排列的,这应该没有问题,oracle官方文档上也是这样说的。

文档在哪里?我没有找到。

11G的例子里面还是用了ORDER BY:
http://download.oracle.com/docs/ ... 8286/queries004.htm

INTERSECT Example
The following statement combines the results with the INTERSECT operator, which returns only those unique rows returned by both queries:
SELECT product_id FROM inventories
INTERSECT
SELECT product_id FROM order_items
ORDER BY product_id;

MINUS Example
The following statement combines results with the MINUS operator, which returns only unique rows returned by the first query but not by the second:
SELECT product_id FROM inventories
MINUS
SELECT product_id FROM order_items
ORDER BY product_id;

它还说未来版本中INTERSECT的优先级会更高:
Note:
To comply with emerging SQL standards, a future release of Oracle will give the INTERSECT operator greater precedence than the other set operators. Therefore, you should use parentheses to specify order of evaluation in queries that use the INTERSECT operator with other set operators.

wiki上也说:
http://en.wikipedia.org/wiki/Set_operations_(SQL)

Note that UNION does not guarantee the order of rows. Rows from the second operand may appear before, after, or mixed with rows from the first operand. In situations where a specific order is desired, ORDER BY must be used.

当然这里讲的不是针对哪个数据库。

使用道具 举报

回复
论坛徽章:
1088
金色在线徽章
日期:2007-04-25 04:02:08金色在线徽章
日期:2007-06-29 04:02:43金色在线徽章
日期:2007-03-11 04:02:02在线时间
日期:2007-04-11 04:01:02在线时间
日期:2007-04-12 04:01:02在线时间
日期:2007-03-07 04:01:022008版在线时间
日期:2010-05-01 00:01:152008版在线时间
日期:2011-05-01 00:01:342008版在线时间
日期:2008-06-03 11:59:43ITPUB年度最佳技术原创精华奖
日期:2013-03-22 13:18:30
202#
 楼主| 发表于 2010-1-22 23:40 | 只看该作者
oracle9i:Advanced sql上是这样说的
P23:
By default:the output is stored in ascending order of the first column of SELECT cluase.
后面一节是Controlling the Order of Rows
是这样说的:
By default,the output is stored in ascending order on  the first column .you can use the Order by clause to change this.

使用道具 举报

回复
论坛徽章:
2
ITPUB十周年纪念徽章
日期:2011-11-01 16:20:282013年新春福章
日期:2013-02-25 14:51:24
203#
发表于 2010-1-22 23:53 | 只看该作者
友情支持一下

使用道具 举报

回复
论坛徽章:
520
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
204#
发表于 2010-1-23 01:01 | 只看该作者
原帖由 dingjun123 于 2010-1-22 23:40 发表
oracle9i:Advanced sql上是这样说的
P23:
By default:the output is stored in ascending order of the first column of SELECT cluase.
后面一节是Controlling the Order of Rows
是这样说的:
By default,the output is stored in ascending order on  the first column .you can use the Order by clause to change this.


你这个我google了半天没找到。还是来看一下TOM的说法(在他的博客一篇回复):
http://tkyte.blogspot.com/2005/08/order-in-court.html

Does UNION do a sort or is it the same as GROUP BY?

Union is a distinct operation and may or may not sort in order to distinct. In any case, the sort it does would be a binary sort, not a character set sort (eg: does not sort the same as order by would necessarily). Throw in parallel operations, partitioning and all bets are off.

另外他在asktom上重复了很多次了,比如这个:
http://asktom.oracle.com/pls/ask ... N_ID:75397449124988

使用道具 举报

回复
论坛徽章:
520
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
205#
发表于 2010-1-23 04:26 | 只看该作者
又看了一些,再提一些意见或补充:

5)找出每个部门的经理
这个需求,我们可以从树中查找,也就是对于每个部门选最高等级节点。可以使用connect
by后加条件过滤branches 的方法。
select level,a.* from
s_emp a
start with manager_id is null
connect by prior id=manager_id and dept_id !=prior dept_id;--当前行的dept_id不等于前一行的
dept_id,即每个子树中选最高等级节点


这个SQL和需求“找出每个部门的经理”没什么关系。找出来的可能有很多非经理。


6)查询一个组织中最高的几个等级
用where level条件过滤
select level,a.* from
s_emp a
where level <=2 –查找前两个等级
start with manager_id is null
connect by prior id=manager_id and dept_id !=prior dept_id;


这个需求把LEVEL<=2放到CONNECT BY中会更高效,因为尽早截断遍历路径。



8)找出指定层次中的叶子节点
Leaf(叶子)就是没有子孙的孤立节点。Oracle 10g提供了一个简单的connect_by_isleaf=1,
0 表示非叶子节点
select level,id,manager_id,last_name, title from s_emp
where connect_by_isleaf=1 –表示查询叶子节点
start with manager_id=2
connect by prior id=manager_id;
Oracle advanced sql by dj mailto:dingjunlove@163.com
第 27 页 共 90 页
也可以通过连接查询获得,方法有多种,叶子节点肯定是level最大的节点。
select a.lev,b.id from (
select max(level) lev from s_emp --查询最大的level
start with manager_id=2
connect by prior id=manager_id) a,
(select level lev,id
from s_emp
start with manager_id=2
connect by prior id=manager_id) b
where a.lev=b.lev;


这方法有问题,因为子树可能不是等高的。矮树的叶子LEVEL较小。



5.3.4 CONNECT_BY_ROOT

9i办法:用SYS_CONNECT_BY_PATH, 然后用INSTR, SUBSTR把第一截解析出来。


5.3.5 使用SIBLINGS关键字排序


9i 办法:

SELECT ... FROM
(SELECT ...
      ,SYS_CONNECT_BY_PATH(LPAD(PAPRENT_ID,20)||LPAD(ORDER_BY_表达式,20),'\') AS sorting  ----- LPAD用于对齐,如果LPADORDER_BY_表达式是字符串,则第二个LPAD不用;如果是日期、时间先TO_CHAR转换为 YYYYMMDDHH24MISS
  FROM ...
START WITH ....
CONNECT BY ....
)
ORDER BY sorting



1、 固定列数的行列转换

name subject grade
---------------------------
student1 语文 80
student1 数学 70
student1 英语 60
student2 语文 90
student2 数学 80
student2 英语 100
……
转换为
语文 数学 英语
student1 80 70 60
student2 90 80 100
按照name分组得到的结果,所以decode外面要套个sum,不然group by出现语义错误,
结果为
select name,sum(decode(subject,'语文', grade,null)) "语文",
sum(decode(subject,'数学', grade,null)) "数学",
sum(decode(subject,'英语', grade,null)) "英语"
from student
group by name


最好用MAX或MIN不要用SUM, 这样也适用于非NUMBER型的行转列。

使用道具 举报

回复
论坛徽章:
1088
金色在线徽章
日期:2007-04-25 04:02:08金色在线徽章
日期:2007-06-29 04:02:43金色在线徽章
日期:2007-03-11 04:02:02在线时间
日期:2007-04-11 04:01:02在线时间
日期:2007-04-12 04:01:02在线时间
日期:2007-03-07 04:01:022008版在线时间
日期:2010-05-01 00:01:152008版在线时间
日期:2011-05-01 00:01:342008版在线时间
日期:2008-06-03 11:59:43ITPUB年度最佳技术原创精华奖
日期:2013-03-22 13:18:30
206#
 楼主| 发表于 2010-1-23 09:56 | 只看该作者
谢谢newkid指教,我试试看!

使用道具 举报

回复
论坛徽章:
56
2010年世界杯参赛球队:瑞士
日期:2010-02-26 11:04:012012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:23版主2段
日期:2012-05-31 02:10:00版主2段
日期:2012-05-31 02:10:00ITPUB 11周年纪念徽章
日期:2012-10-09 18:05:07现任管理团队成员
日期:2012-10-18 18:22:36
207#
发表于 2010-1-23 11:35 | 只看该作者
好东西,多谢楼主分享。

使用道具 举报

回复
论坛徽章:
0
208#
发表于 2010-1-24 15:19 | 只看该作者
这个帖子不顶不行

使用道具 举报

回复
论坛徽章:
1
ITPUB十周年纪念徽章
日期:2011-11-01 16:24:51
209#
发表于 2010-1-25 07:22 | 只看该作者
12.ROWID的使用        71
13. ORACLE10G正则表达式        71
14.使用HINT        71
15.PARITION分区        71
16.并行操作        71

这几个没写啊

使用道具 举报

回复
论坛徽章:
1
ITPUB十周年纪念徽章
日期:2011-11-01 16:24:51
210#
发表于 2010-1-25 07:42 | 只看该作者
好东西,多谢楼主分享

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表