如何加入两个SELECT语句?

时间:2012-01-28 18:01:28

标签: sql oracle

select * 
From
(
    select * 
    from order 
    order by creationtime desc
) 
where rownum=1 and creationtime='12-feb-2010';

select * 
from
(
    select * 
    from 
    order by rate desc
) 
where rownum<=2 and creationtim='12-dec-2011';

我想使用SELECT加入这两个JOIN个查询。两个SELECT查询来自同一个表的查询。我不想使用UNION

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:8)

很难从你的问题中看出下面的“ON”条款应该是什么,因为你没有指出主键,但这应该让你知道你需要做什么。

select * From
(select * from order order by creationtime desc) A 
INNER JOIN (select * from order by rate desc) B
ON A.rownum = B.rownum
where A.rownum=1 and A.creationtime='12-feb-2010' 
AND B.rownum<=2 and B.creationtim='12-dec-2011'

答案 1 :(得分:1)

使用row_number()分析函数获取每个值的第一行。

select [list of columns]
  from (select o.*, row_number() over (partition by creationtime)
          from order o
         where creatontime in ('12-DEC-2011', '12-FEB-2010')
       )
 where rn = 1;