简体   繁体   English

具有三个或更多选择的UNION ALL正确语句

[英]Correct statement for UNION ALL with three or more selects

I have the following situation: 我有以下情况:

I have a script consisting of 6 selects joined by "UNION ALL". 我有一个由“ UNION ALL”加入的6个选择组成的脚本。

From the CLP DB2 console, this script fails. 从CLP DB2控制台,此脚本失败。 Curiously, each query independently work, and even come to work if grouped in pairs. 奇怪的是,每个查询都是独立工作的,甚至成对分组也可以工作。 However, when I try with three or more, it fails. 但是,当我尝试三个或更多时,它将失败。

So, my question is: is there is a limit for more that one UNION ALL? 因此,我的问题是:一个UNION ALL是否有限制?

My environment is: 我的环境是:

  • Client. 客户。 DB2 Connect server 10.1 DB2 Connect服务器10.1
  • zOS 390 (no idea what is the DB2 version on that side) zOS 390(不知道那边的DB2版本是什么)
  • AIX 7.1 AIX 7.1

The query is like this (but three times ) 查询是这样的(但是三遍)

SELECT
        ,'GG'
    ,varchar(right( '000000000000000' || rtrim(ltrim(eeee.zzzz)), 15), 15)  
        ,substr(char(right('**********'||char(left(replace(eeee.yyyy,' ','*')||'**********',10),10),10),10),1,7)
        ,eeee.kkkkk
        ,eeee.hhhhhh
            ,CASE WHEN hhhhhh='A5 ' THEN 'ARS' WHEN hhhhhh='A6 ' THEN 'AUD' WHEN hhhhhh='B5 ' THEN 'BRL' WHEN hhhhhh='U1 ' THEN 'GBP' WHEN hhhhhh='B9 ' THEN 'BND' WHEN hhhhhh='B6 ' THEN 'BNG' WHEN hhhhhh='C1 ' THEN 'CAD' WHEN hhhhhh='C3 ' THEN 'CLP' WHEN hhhhhh='C4 ' THEN 'CNY' WHEN hhhhhh='C5 ' THEN 'COP' WHEN hhhhhh='C7 ' THEN 'CRC' WHEN hhhhhh='L5 ' THEN 'HRK' WHEN hhhhhh='C9 ' THEN 'CYP' WHEN hhhhhh='X0 ' THEN 'CZK' WHEN hhhhhh='D0 ' THEN 'DKK' WHEN hhhhhh='D1 ' THEN 'DOP' WHEN hhhhhh='U0 ' THEN 'EGP' WHEN hhhhhh='E3 ' THEN 'EUR' WHEN hhhhhh='G5 ' THEN 'GTQ' WHEN hhhhhh='H0 ' THEN 'HTG' WHEN hhhhhh='H3 ' THEN 'HUF' WHEN hhhhhh='I1 ' THEN 'INR' WHEN hhhhhh='I2 ' THEN 'IDR' WHEN hhhhhh='K2 ' THEN 'WON' WHEN hhhhhh='L6 ' THEN 'LVL' WHEN hhhhhh='L7 ' THEN 'LTL' WHEN hhhhhh='M2 ' THEN 'MYR' WHEN hhhhhh='M6 ' THEN 'MXN' WHEN hhhhhh='I8 ' THEN 'ILS' WHEN hhhhhh='N2 ' THEN 'NZD' WHEN hhhhhh='N4 ' THEN 'NIO' WHEN hhhhhh='N6 ' THEN 'NOK' WHEN hhhhhh='T4 ' THEN 'XPF' WHEN hhhhhh='P0 ' THEN 'PKR' WHEN hhhhhh='P1 ' THEN 'PAB' WHEN hhhhhh='P3 ' THEN 'PEN' WHEN hhhhhh='P4 ' THEN 'PHP' WHEN hhhhhh='P5 ' THEN 'PLN' WHEN hhhhhh='R2 ' THEN 'RON' WHEN hhhhhh='U3 ' THEN 'RUB' WHEN hhhhhh='S0 ' THEN 'SAR' WHEN hhhhhh='R6 ' THEN 'RSD' WHEN hhhhhh='S2 ' THEN 'SGD' WHEN hhhhhh='K5 ' THEN 'SKK' WHEN hhhhhh='S4 ' THEN 'ZAR' WHEN hhhhhh='C2 ' THEN 'LKR' WHEN hhhhhh='S8 ' THEN 'SEK' WHEN hhhhhh='S9 ' THEN 'CHF' WHEN hhhhhh='T2 ' THEN 'THB' WHEN hhhhhh='T6 ' THEN 'TRL' WHEN hhhhhh='U4 ' THEN 'USD' WHEN hhhhhh='U6 ' THEN 'UAH' WHEN hhhhhh='U5 ' THEN 'AED' WHEN hhhhhh='U2 ' THEN 'UYU' WHEN hhhhhh='V0 ' THEN 'VEB' WHEN hhhhhh='V1 ' THEN 'VND' WHEN hhhhhh='J1 ' THEN 'JPY' ELSE '###' END
        ,  case when eeee.FCRCIDF='Y' then 1 else 0 end
        ,
                    CASE
                    WHEN SUBSTR(eeee.yyyy,7,1) = 'X' THEN 'X'       
                    WHEN SUBSTR(eeee.yyyy,4,2) = 'O' THEN 'O'       
                    WHEN SUBSTR(eeee.yyyy,4,2) = 'C' THEN 'C'       
                    WHEN SUBSTR(eeee.yyyy,4,2) = 'R' THEN 'R'       
                    WHEN eeee.lll = 'F' THEN 'F'            
                    WHEN eeee.ppp <> '' THEN 'D'            
                    WHEN eeee.rrr = 0 THEN '0'          
                    WHEN eeee.rrr <> eeee.ACINTOT THEN 'P'      
                    WHEN eeee.rrr = eeee.ACINTOT THEN '1'           
                    ELSE '*'                    
                    END
        ,1
        ,eeee.DCINISS
        ,0
        from  (SELECT ori.*,oric.tttt FROM www.SK1V01_CUSTOMER ori left OUTER JOIN www.SK1V01A_CUSTCUF oric
      ON ori.bbbb=oric.bbbb  and   ori.ICUSCNO=oric.ICUSCNO ) as aaaa
             ,www.SK1V02_OPENBILL eeee,www.SK1V41_OPENBILL kkkk
         where      aaaa.bbbb=eeee.bbbb
              and   aaaa.cagllic=eeee.cagllic
              and   aaaa.icuscno=eeee.icuscno

Without the entire statement its pretty hard to determine exact reasons. 没有整个陈述,很难确定确切原因。 An given that just one portion is so long & poorly formatted, I'm not sure we'd want to dig through it all. 鉴于只有一部分很长且格式不正确,因此我不确定我们是否想一探究竟。 But I can suggest a few approaches that may help resolve your problem. 但我可以建议一些可能有助于解决问题的方法。

Simplest part first. 最简单的部分优先。 In practically any computer language, well formatted code helps you see the structure of what's going on. 在几乎任何计算机语言中,格式良好的代码都可以帮助您了解正在发生的事情的结构。 It may also help you spot the differences between your queries. 它还可以帮助您发现查询之间的差异。 (Perhaps you know this, & your code merely lost its formatting when you tried to post it.) (也许您知道这一点,并且您的代码在尝试发布时只是失去了其格式。)

When trying to UNION multiple complex queries, it's not uncommon to have column inconsistencies among the queries. 当尝试对多个复杂查询进行UNION时,查询之间的列不一致并不罕见。 You might have missing or extra columns, or columns out of order. 您可能缺少或多余的列,或者列混乱。 But it's possible some of your column expressions are evaluating to different types. 但是您的某些列表达式可能会针对不同的类型求值。 You might want to cast() those expressions, or use type conversion functions, just to be sure. 可以肯定的是,您可能想要强制转换()这些表达式,或使用类型转换函数。

There's so much going on here. 这里有很多事情。 Try testing with a version where you comment out large chunks of code, same on each major subquery, until you find which part is causing the problem. 尝试使用注释掉大量代码的版本进行测试,每个主要子查询中的注释都相同,直到找到导致问题的部分。

You have a ridiculously long CASE expression on hhhhhh. 您在hhhhhh上有一个荒谬的CASE表达式。 Why don't you put these value pairs into a lookup table that you can join to. 为什么不将这些值对放入可以加入的查找表中。

Try using a module approach, just as you should when writing a large program. 尝试使用模块方法,就像编写大型程序时一样。 You could create a view for each of the major queries, then UNION them together. 您可以为每个主要查询创建一个视图,然后将它们联合在一起。 (Some developers use layers of views like layers of modular code). (一些开发人员使用视图层,例如模块化代码层)。

Metadata about your views is available in the database catalog views. 有关您的视图的元数据可在数据库目录视图中找到。 This means you could write a query to compare the attributes of the columns in your set of union views. 这意味着您可以编写查询以比较联合视图集中的列的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM