[Oracle]高效的SQL语句之分析函数(二)
本系列文章导航
[Oracle]高效的SQL语句之分析函数(一)--sum()
[Oracle]高效的SQL语句之分析函数(二)--max()
[Oracle]高效的SQL语句之分析函数(三)--row_number() /rank()/dense_rank()
[Oracle]高效的SQL语句之分析函数(四)--lag()/lead()
如果我们按照示例想得到每个部门薪水值最高的雇员的纪录,可以有四种方法实现:
先创建示例表
create table emp
as
select * from scott.emp;
alter table emp
add constraint emp_pk
primary key(empno);
create table dept
as
select * from scott.dept;
alter table dept
add constraint dept_pk
primary key(deptno);
方法1.emp中的每一行都会进行max比较,费时
select * from emp emp1 where emp1.sal=(select max(emp2.sal) from emp emp2 where emp2.deptno=emp1.deptno)
方法2.先子查询查找出max sal,然后与emp表相关联,如果逻辑复杂会产生较多代码
select * from emp emp1,(select deptno,max(sal) maxsal from emp emp2 group by emp2.deptno) emp3 where emp1.deptno=emp3.deptno and emp1.sal=emp3.maxsal
方法3.使用max分析函数
select deptno,maxsal,empno from(
select max(sal) over (partition by deptno) maxsal,emp.* from emp) emp2
where emp2.sal=emp2.maxsal
方法4.使用dense_rank分析函数,如果一个部门可能存在多笔最大薪水,就不能使用row_number()分析函数
select deptno,sal,empno from(
select emp.*,DENSE_RANK() over (partition by deptno order by sal desc) rownumber from emp) emp2
where rownumber=1
结果如下:
10 5000.00 7839
20 3000.00 7788
20 3000.00 7902
30 2850.00  
相关文档:
在Excel中,我们时常会碰到这样的字段(最常见的就是电话号码),即有纯数字的(如没有带区号的电话号码),又有数字和其它字符混合 (如“区号-电
话号码”)的数据,在导入SQLServer过程中,会发现要么纯数字的数据导过去之后变成了NULL,要么就是数字和其它字符混合的数据导过去之后变成
了NULL。
&n ......
在DELPHI中常常要用到ADOQUERY中的SQL语句增加信息后执行,当要求增加条件(如参数时)中间的间隔很重要,搞不好就要出错:下举例说明我要表达的意思:
如:一个表:student 有 ksh,xm,xb,lqzy,lqcc几个字段,都为字符型。现要求按xb分组统计人数,同时lqcc要求限定条件。
一般情况下的SQL语句应该为: select xb,count(*) ......
1.在查询分析器下查询Excel文档
Select * from
OpenDataSource('Microsoft.Jet.OLEDB.4.0','Data Source = "c:\测试.xls";User ID = Admin;Password=;Extended properties=Excel8.0)....Sheet1$
2.从数据库中导出数据并存到文件中
EXEC master..xp_cmdshell 'bcp CAS2004..HGZ_LIAOJIAN out c:\temp1.xls -c -q -S"."- ......
在使用ODP.NET进行Oracle编程时,有时候SQL语句非常复杂,需要采用动态构造查询语句的情况,有两种方法可以构造动态的SQL语句,并执行返回结果集。
1、在数据访问层构造SQL语句
例如下面的语句,将构造完整的SQL语句赋值给CommandText,再传递到数据库进行执行,返回结果集。
loadCommand.CommandType = Com ......