Oracle update和order by
今天遇到一个关于SQL转换成Oracle语句的问题,描述如下:
select * from emp order by deptno;

select * from dept;

Sql Server:
update dept a set dname=(select top 1 ename from emp where deptno=a.deptno order by sal)
经过尝试,查找资料,得出下面转换结果,不知道这样是否可行:
update dept a set dname=
(with t as(select ename,deptno from emp order by sal)
select ename from t where deptno=a.deptno and rownum=1)
where exists(with t as(select ename,deptno from emp order by sal)
select null from t where deptno=a.deptno)
执行结果:
select * from dept;

cost:22
另外一种:
update dept a
set dname =
(select max(ename)
from emp
where deptno = a.deptno
and sal = (select min(sal) from emp where deptno=a.deptno))
where exists (select null
from emp
where deptno = a.deptno);
这种耗cost比第一个更大。
cost:27
第三种:
update dept a
set dname =
(select col
from (select decode(row_number()
over(partition by deptno order by sal),
1,
ename,
null) col,
deptno
from emp) b
where b.deptno = a.deptno
and b.col is not null)
where exists (select null from emp where deptno = a.deptno);
cost:21
已找到较简洁的方法:
update dept a
set dname =
(select max(ename) keep(dense_rank first order by nvl(sal, 0))
from emp
where deptno = a.deptno)
where exists (select null from emp where deptno = a.deptno);
cost:18
Oracle update和order by的更多相关文章
- Oracle Update 语句语法与性能分析 - 多表关联
Oracle Update 语句语法与性能分析 - 多表关联 为了方便起见,建立了以下简单模型,和构造了部分测试数据: 在某个业务受理子系统BSS中, SQL 代码 --客户资料表 create ...
- 在SQL Server中如何进行UPDATE TOP .....ORDER BY?
前言 今天在导入数据到系统后需要根据时间排序对刚导入的TOP N条进行数据更新,之前没遇到过UPDATE TOP...ORDER BY,以此作为备忘录. SQL SERVER之UPDATE TOP.. ...
- oracle数据库访问order by不起作用分析
`SELECT * FROM student ROWNUM <= 1 ORDER BY id ASC`执行结果,返回结果没有排序.使用驱动"System.Data.OracleClie ...
- oracle update left join 写法
oracle update left join 写法 (修改某列,条件字段在关联表中) 案例: E:考核表 X,:用户表 USERNAME 关联 需求:修改营业部最高分 分析:通过登录账号的营业部OR ...
- Oracle update 多字段更新
一次性update多个字段 以student表为例: -- 创建学生表 create table student ( id number, name varchar2(40), age number, ...
- Oracle update和select 关联
Oracle update和select 关联 目录 Oracle update和select 关联 1.介绍 2.解决方法 2.1.需求 2.2.错误演示 2.3.解决方法 1.介绍 本文主要向大家 ...
- Oracle update 两表及以上关联更新,出现多值情况,不是一对一更新
为了方便起见,建立了以下简单模型,和构造了部分测试数据:在某个业务受理子系统BSS中, SQL 代码--客户资料表 create table customers ( customer_id numbe ...
- Oracle Update
在表的更新操作中,在很多情况下需要在表达式中引用要更新的表以外的数据.象sql server提供了update的from 子句,可以将要更新的表与其它的数据源连接起来.虽然只能对一个表进行更新,但是通 ...
- [转]oracle update set select from 关联更新
本文转自:http://blog.csdn.net/disiwei1012/article/details/52589181 http://www.blogjava.net/Jhonney/archi ...
随机推荐
- Hadoop2.2.0安装过程记录
1 安装环境1.1 客户端1.2 服务端1.3 安装准备 2 操作系统安装2.1.1 BIOS打开虚拟化支持2.1.2 关闭防火墙2.1.3 安装 ...
- C#委托使用详解(Delegates)
摘要 委托是C#编程一个非常重要的概念,也是一个难点.本文将系统详细讲解委托. 1. 委托是什么? 其实,我一直思考如何讲解委托,才能把委托说得更透彻.说实话,每个人都委托都有不同的见解,因为看问题的 ...
- Web网站中利用JavaScript中ActiveXObject对象获取硬件信息(显示器数量、分辨率)从而进行单双屏跳转
前言:最近这两天工作上,要实现一个功能,在好友阿聪的帮助下,算是比较好的解决了这个需求. B/S的Web网站,需要实现点击按钮时,根据客户端连接的显示屏(监视器)数量进行,单双屏跳转显示新页面. 由于 ...
- 利用Docker技术实现UDP广播效果(网络编程python版)
docker的安装见官方文档 我使用的系统为Ubuntu16.04 Ubuntu系统安装docker文档地址:https://docs.docker.com/engine/installation/l ...
- javascript中的函数式声明与变量式声明
观察下面两段代码,试写出hello('word');的运行结果: // 变量式声明 function hello(msg){ alert(msg); var msg = function(){}; a ...
- 学习总结 之 WebApi服务监控 log4net记录监控日志
在请求WebApi 的时候,我们更想知道在请求数据的时候,调用了哪个接口传了什么参数过来,调用这个Action花了多少时间,有没有人恶意请求.我们可以通过记录日志,对Action进行优化,可以通过日志 ...
- Java 特定规则排序-LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- u-boot的配置、编译及链接
第一次写技术博客,还有些兴奋呢.我是CrazyCatJack,大家可以叫我CCJ或者疯猫.我即将成为一名嵌入式Linux的驱动工程师,现在还是一枚大四狗,呼呼~大学期间做了一些项目和比赛,都是基于32 ...
- SQL Server索引视图以(物化视图)及索引视图与查询重写
本位出处:http://www.cnblogs.com/wy123/p/6041122.html 经常听Oracle的同学说起来物化视图,物化视图的作用之一就是可以实现查询重写,听起来有一种高大上的感 ...
- DOM 事件深入浅出(二)
在DOM事件深入浅出(一)中,我主要给大家讲解了不同DOM级别下的事件处理程序,同时介绍了事件冒泡和捕获的触发原理和方法.本文将继续介绍DOM事件中的知识点,主要侧重于DOM事件中Event对象的属性 ...