繁体   English   中英

在 oracle 11 的子查询中使用 rownum

[英]Using rownum in sub query on oracle 11

我有这样的查询。它在 oracle 12 上正常工作。

select * from customers where customerId IN (select custId from Orders where 
   orderStatus = 'S' and orderCity = 'Amsterdam' and ORDER BY custId DESC FETCH FIRST 10 ROWS ONLY)

但我正在使用 oracle 11.该查询不适用于 oracle 11.因此我改变了我的查询

select * from customers where customerId IN (select custId from Orders where 
  orderStatus = 'S' and orderCity = 'Amsterdam' and ORDER BY custId DESC rownum <= 10)

它给出了missing right paranthesis

我该如何解决这个问题。你有什么想法吗?实际上我在 10 数字上使用了一个变量。

语法有点不同 - 您需要一个额外的子查询,因此它需要更像... where customerId IN (select * from (select custId from Orders where orderStatus = 'S' and orderCity = 'Amsterdam' and ORDER BY custId DESC) where rownum <=10)

如果您没有 order by 子句,则不需要额外的子查询

For ref see https://docs.oracle.com/cd/B19306_01/server.102/b14200/pseudocolumns009.htm#:~:text=For%20each%20row%20returned%20by,has%202%2C%20and% 20so%20on

我会用existsrow_number()来表达这个:

select c.* 
from customers 
where exists (
    select 1
    from (
        select custId, row_number() over(order by custid desc) rn
        from orders 
        where orderStatus = 'S' and orderCity = 'Amsterdam'
    ) o
    where o.rn <= 10 and o.cusid = c.customerid
)

row_number()在子查询中按客户 ID 降序排列订单。 然后,我们过滤前 10 行,并使用exists过滤外部查询中的相应行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM