[英]Using rownum in sub query on oracle 11
I have query like this.It is working properly on oracle 12.我有这样的查询。它在 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)
But I am using oracle 11.That query is not working on oracle 11.Therefore I changed my query like that但我正在使用 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)
It gives missing right paranthesis
它给出了
missing right paranthesis
How can I solve this problem.Do you have any idea?Actually I use a variable instead on 10 number.我该如何解决这个问题。你有什么想法吗?实际上我在 10 数字上使用了一个变量。
Syntax is a bit different - you need an extra subquery so it would need to be more like... where customerId IN (select * from (select custId from Orders where orderStatus = 'S' and orderCity = 'Amsterdam' and ORDER BY custId DESC) where rownum <=10)
语法有点不同 - 您需要一个额外的子查询,因此它需要更像...
where customerId IN (select * from (select custId from Orders where orderStatus = 'S' and orderCity = 'Amsterdam' and ORDER BY custId DESC) where rownum <=10)
You wouldn't need the extra subquery if you didn't have the order by clause如果您没有 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 . 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 。
I would phrase this with exists
and row_number()
:我会用
exists
和row_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()
ranks orders by descending customer id in the subquery. row_number()
在子查询中按客户 ID 降序排列订单。 Then, we filter on the first 10 rows, and use exists
to filter the corresponding rows in the outer query.然后,我们过滤前 10 行,并使用
exists
过滤外部查询中的相应行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.