2
WITH DATA
     AS ( select ACCT_ID_1,  ACCT_ID, ACCT_ID_2 ... from two tables joined by criteria ),
DATA1
     AS (SELECT D1.*
           FROM DATA D1
          WHERE ACCT_ID_1 = ACCT_ID AND ACCT_ID_1 = '101'),
DATA2
     AS (SELECT D2.*
           FROM DATA D2
          WHERE ACCT_ID_2 = ACCT_ID AND ACCT_ID_2 = '101')

data1にデータがある場合(data2からのデータがない場合)はdata1を取得する必要があります。それ以外の場合は、data2から取得する必要があります。SQLを使用することは可能ですか?

4

2 に答える 2

4

これはおそらく私がそれを行う方法です:

WITH data1 AS (
  SELECT 1
  FROM dual
  WHERE 1=2 -- Set to 1 to reverse result
)
, data2 AS (
  SELECT 2
  FROM dual  
)
SELECT *
FROM data1
UNION ALL
SELECT *
FROM data2
WHERE NOT EXISTS (
  SELECT 1
  FROM data1
)

例としてSQLFiddleに投稿されました。

于 2012-11-20T22:30:54.863 に答える
0
SELECT ACCT_ID_1,  ACCT_ID, ACCT_ID_2, ...,
  IF EXISTS (SELECT D1.*
       FROM DATA D1
      WHERE ACCT_ID_1 = ACCT_ID AND ACCT_ID_1 = '101')
  THEN (SELECT D1.ACCT_ID_1
       FROM DATA D1
      WHERE ACCT_ID_1 = ACCT_ID AND ACCT_ID_1 = '101')
  ELSE (SELECT D2.ACCT_ID_2
       FROM DATA D2
       WHERE ACCT_ID_2 = ACCT_ID AND ACCT_ID_2 = '101')
  END IF
于 2012-11-20T22:18:46.683 に答える