SQL:如果第一个查询没有返回任何行,则调用第二个查询

时间:2012-11-28 15:39:58

标签: sql database join db2

我正在努力将这两个陈述合并为一个陈述。

我的第一个语句实际上是3个语句,其中我只想要第一个返回要返回的值的语句,这样可以处理。

SELECT ReferenceKey, ReferenceValue FROM
(   
    SELECT a.GBNK076 AS ReferenceKey, TRIM(a.GBNK076) ||' - '||  b.BANM11 AS ReferenceValue, 1 as preference 
    FROM THTFU.THAP076P AS a LEFT OUTER JOIN OSLTHLF3.CSP11  AS b ON  b.BANK11 = a.GBNK076  AND b.CONO11 = a.CONO076 
    WHERE a.CONO076 = '01' AND a.PMTH076 = 'BMG' AND a.CURN076 = 'EUR'  
    UNION
    SELECT a.GBNK076 AS ReferenceKey, TRIM(a.GBNK076) ||' - '||  b.BANM11 AS ReferenceValue, 3 as preference 
    FROM THTFU.THAP076P AS a LEFT OUTER JOIN  OSLTHLF3.CSP11  AS b ON  b.BANK11 = a.GBNK076 AND b.CONO11 = a.CONO076 
    WHERE a.CONO076 = '01' AND a.PMTH076 = 'BMG' AND a.CURN076 = ''
    UNION
    SELECT a.GBNK076 AS ReferenceKey, TRIM(a.GBNK076) ||' - '||  b.BANM11 AS ReferenceValue, 3 as preference 
    FROM THTFU.THAP076P AS a LEFT OUTER JOIN  OSLTHLF3.CSP11  AS b ON  b.BANK11 = a.GBNK076 AND b.CONO11 = a.CONO076 
    WHERE a.CONO076 = '01' AND a.PMTH076 = '' AND a.CURN076 = ''
)BankPayingFrom ORDER BY preference
FETCH FIRST 1 ROWS ONLY

但是,如果这三个语句都没有返回任何信息,我希望能够触发下面的SQL语句来检索所有可能的记录。

SELECT BANK11 as ReferenceKey, TRIM(BANK11) ||' - '|| BANM11 as ReferenceValue From OSLTHLF3.CSP11 WHERE CONO11 = '01'

如何将这两个语句连接在一起,这样我只需要从我的Web服务调用一个到后端数据库(AS / 400 DB2数据库)?

我很感激你的帮助。

非常感谢 基督教

2 个答案:

答案 0 :(得分:1)

在不知道您的数据集的情况下,很难确切知道,但我相信这可能是一个更清洁的选择。 可能表现得更快,但我无法保证。

WITH  BankPayingFrom (referenceKey, referenceValue) as (
      SELECT a.GBNK076, TRIM(b.BANK11) ||' - '||  b.BANM11 -- typo? 'b.BANK11'?
      FROM THTFU.THAP076P as a
      LEFT JOIN OSLTHLF3.CSP11 as b
                ON b.BANK11 = a.GBNK067
                   AND b.CONO11 = a.CONO076
      WHERE a.CONO076 = '01'
            AND ((a.PMTH076 = 'BMG' AND a.CURN076 = 'EUR')
                 OR (a.PMTH076 = 'BMG' AND a.CURN076 = '')
                 OR (a.PMTH076 = '' AND a.CURN076 = ''))
            -- You may be able to use the following, but only if
            -- a.PMTH076 is set for every set value of a.CURN076
               -- AND (a.PMTH076 = 'BMG' OR a.PMTH067 = '')
               -- AND (a.CURN076 = 'EUR' OR a.CURN076 = '')
      ORDER BY a.PMTH076 DESC, a.CURN076 DESC
      FETCH FIRST 1 ROW ONLY)

SELECT referenceKey, referenceValue 
FROM BankPayingFrom
UNION 
SELECT BANK11 as referenceKey, TRIM(BANK11) ||' - '|| BANM11 as referenceValue
FROM  OSLTHLF3.CSP11
WHERE CONO11 = '01'
      AND NOT EXISTS (SELECT '1'
                      FROM BankPayingFrom)

......虽然进一步观察,但CTE中唯一真正的区别是a.GBNK076。如果THAP076P中存在“匹配”行,您只需要1行,否则您想要所有这些行吗?

答案 1 :(得分:0)

不确定AS / 400(iSeries),但这是我在Linux Unix Windows上的表现方式:

WITH BankOrderFromOne(ReferenceKey, ReferenceValue)
AS (
    SELECT ReferenceKey, ReferenceValue FROM
    (   
        SELECT a.GBNK076 AS ReferenceKey, TRIM(a.GBNK076) ||' - '||  b.BANM11 AS ReferenceValue, 1 as preference 
        FROM THTFU.THAP076P AS a LEFT OUTER JOIN OSLTHLF3.CSP11  AS b ON  b.BANK11 = a.GBNK076  AND b.CONO11 = a.CONO076 
        WHERE a.CONO076 = '01' AND a.PMTH076 = 'BMG' AND a.CURN076 = 'EUR'  
        UNION
        SELECT a.GBNK076 AS ReferenceKey, TRIM(a.GBNK076) ||' - '||  b.BANM11 AS ReferenceValue, 3 as preference 
        FROM THTFU.THAP076P AS a LEFT OUTER JOIN  OSLTHLF3.CSP11  AS b ON  b.BANK11 = a.GBNK076 AND b.CONO11 = a.CONO076 
        WHERE a.CONO076 = '01' AND a.PMTH076 = 'BMG' AND a.CURN076 = ''
        UNION
        SELECT a.GBNK076 AS ReferenceKey, TRIM(a.GBNK076) ||' - '||  b.BANM11 AS ReferenceValue, 3 as preference 
        FROM THTFU.THAP076P AS a LEFT OUTER JOIN  OSLTHLF3.CSP11  AS b ON  b.BANK11 = a.GBNK076 AND b.CONO11 = a.CONO076 
        WHERE a.CONO076 = '01' AND a.PMTH076 = '' AND a.CURN076 = ''
    )BankPayingFrom ORDER BY preference
    FETCH FIRST 1 ROWS ONLY
)

SELECT ReferenceKey, ReferenceValue FROM BankOrderFromOne

UNION

SELECT BANK11 as ReferenceKey, TRIM(BANK11) ||' - '|| BANM11 as ReferenceValue From OSLTHLF3.CSP11 WHERE CONO11 = '01'
AND NOT EXISTS (SELECT ReferenceKey, ReferenceValue FROM BankOrderFromOne)