[英]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 子句,則不需要額外的子查詢
我會用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()
在子查詢中按客戶 ID 降序排列訂單。 然后,我們過濾前 10 行,並使用exists
過濾外部查詢中的相應行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.