Can we use Order By clause when we are using Select Query in Select List ?
I want to write query like this :-
select col1
, col2
, (select colX from table2 where table2.col1=table1.col1 and rownum<2 order by colY desc)
, (select colXX from table3 where table3.col1=table1.col1 and rownum<2 order by colYY desc)
from table1;
I can’t join the table. Its just example of what I want.
OK. Here is the thing.
I have following code written in SQL Server . I want to convert it into Oracle.
SELECT
BANK_ADDRESS1= (SELECT top 1 OWNER_ADDRESS1
FROM OWNER_ADDRESS OA, BANK_ACCOUNT
WHERE PAYMENT.BANK_ACCOUNT_ID = BANK_ACCOUNT.BANK_ACCOUNT_ID
and BANK_ACCOUNT.BANK_ID = OA.OWNER_ID
order by Isnull (OA.primary_addr , 'N') desc),
PAYEE_ADD1 = ( SELECT top 1 OWNER_ADDRESS1
FROM OWNER_ADDRESS OA
WHERE OA.OWNER_ID = PAYEE_OWNER.OWNER_ID
order by Isnull (OA.primary_addr , 'N') desc ),
FROM PAYMENT
inner JOIN PAYEE ON PAYMENT.PAYMENT_ID = PAYEE.PAYMENT_ID
inner join OWNER PAYEE_OWNER on PAYEE_OWNER.OWNER_ID = PAYEE.PAYEE_NAME_ID
Hope this will clear what I really want.
You want something like this:
or this:
This subselect
even if it worked would not return what you expect because rownum is calculated before order by. You would get random row from those which fulfill the condition table2.col1 = table1.col1 .
Try
(I could have made some syntax errors, cannot check it).