0

SelectListでSelectQueryを使用しているときに、Order By句を使用できますか?

このようなクエリを書きたい:-

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;

テーブルに参加できません。それは私が欲しいもののほんの一例です。

わかった。これがそのことです。私はSQLServerで書かれた次のコードを持っています。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

これが私が本当に欲しいものをクリアすることを願っています。

4

1 に答える 1

2

あなたはこのようなものが欲しいです:

select col1,
       col2,
       (select colX
          from (select col1, colX from table2 order by colY desc) table2
         where table2.col1 = table1.col1
           and rownum < 2),
       (select colXX
          from (select col1, colXX from table3 order by colYY desc) table3
         where table3.col1 = table1.col1
           and rownum < 2)
  from table1;

またはこれ:

select col1,
       col2,
       (select max(colX) keep(dense_rank last order by colY)
          from table2
         where table2.col1 = table1.col1),
       (select max(colXX) keep(dense_rank last order by colYY)
          from table2
         where table2.col1 = table1.col1)
  from table1;

この副選択

(select colX from table2 where table2.col1=table1.col1 and rownum<2 order by colY desc)

たとえそれが機能したとしても、rownumは注文前に計算されるため、期待したものは返されません。table2.col1=table1.col1の条件を満たす行からランダムな行を取得します。

試す

select (SELECT max(OWNER_ADDRESS1) keep(dense_rank last order by nvl(oa.primary_addr, 'N'))
          FROM OWNER_ADDRESS OA, BANK_ACCOUNT
         WHERE PAYMENT.BANK_ACCOUNT_ID = BANK_ACCOUNT.BANK_ACCOUNT_ID
           and BANK_ACCOUNT.BANK_ID = OA.OWNER_ID) bank_address1,
       (SELECT max(OWNER_ADDRESS1) keep(dense_rank last order by nvl(oa.primary_addr, 'N'))
          FROM OWNER_ADDRESS OA
         WHERE OA.OWNER_ID = PAYEE_OWNER.OWNER_ID) payee_add1
  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

(構文エラーが発生した可能性があり、確認できません)。

于 2012-08-24T10:52:40.783 に答える