1

我正在努力将我放在一起的这两个语句组合成一个语句。

我的第一个语句实际上是 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 数据库)?

我会很感激任何帮助。

非常感谢克里斯蒂安

4

2 回答 2

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)

...尽管进一步观察,唯一真正的区别在于a.GBNK076CTE。如果存在“匹配”行,您是否只需要 1 行THAP076P,否则您想要所有行?

于 2012-11-29T17:33:47.713 回答
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)
于 2012-11-28T19:29:56.313 回答