迁移SQL Server将查询应用于Oracle

时间:2015-06-12 10:57:36

标签: sql-server oracle

在Oracle中,下面的Sql Server查询等同于什么:

select dd.dname, e.ename
from emp e
outer apply 
(select top 1 dname from dept d where d.did=e.did order by bdate) dd

请注意,实际查询非常不同,但概念是相同的。请原谅我上面的查询中的任何语法错误。

我在Oracle查询下面尝试过:

select dd.dname, e.ename
from emp e
left join 
(select * from 
(select dname from dept d where d.did=e.did order by bdate)
where rownum=1) dd

但是,它给出了以下错误:

Error at Command Line:6 Column:18
Error report:
SQL Error: ORA-00905: missing keyword
00905. 00000 -  "missing keyword"
*Cause:    
*Action:

2 个答案:

答案 0 :(得分:1)

加入后

On子句丢失了。因此,我将查询转换为逐个查询,它可以工作:

select dd.dname, e.ename
from emp e
left join 
(select min(bdate), dname, did from dept d group by dname, did)dd
on dd.did=e.did

答案 1 :(得分:0)

您可以先排列行,然后排第一行:

select e.ename, d.dname
  from emp e
  left join  (
      select dept.dname, dept.did, row_number()
         over (partition by did order by bdate) rn from dept) d
    on e.did = d.did and d.rn=1

SQLFiddle