简体   繁体   English

联合所有不提供第二组的列\\名称

[英]Union ALL not giving the column\ names for second group

I am using the tool SQLDBx. 我正在使用工具SQLDBx。 which i highly like btw. 我非常喜欢顺便说一句。 I have 5 groups of series of Select statements and then I made a UNION ALL after the first group. 我有5组Select语句系列,然后在第一组之后创建了UNION ALL。 Now it does run correctly and displays the output but it does not assign the second group' s field names. 现在,它确实可以正确运行并显示输出,但未分配第二组的字段名称。

For example. 例如。 I am greatly abridging 我大大的

Completed the SQL's needed to mimic a IBM i display screen. 完成了模仿IBM i显示屏所需的SQL。 There are 5 product groups making up the screen. 屏幕共有5个产品组。 I was hoping to have one large Command SQL having the 5 SQLs using UNION ALL. 我希望有一个大型命令SQL,其中5个SQL使用UNION ALL。 This does 'compile' as a Command. 这确实作为命令“编译”。 However, it does only brings in the first part fields not the second. 但是,它仅引入第一部分字段,而不引入第二部分字段。 So this field is not included in the list of fields tree for COMMAND. 因此,该字段不包括在COMMAND的字段列表中。 PROGR2R2PST, PROGR2R2PST,

Is there something not correct how doing the UNION ALL? 有一些不正确的方法来执行UNION ALL吗? OUTPUT assigns the column name OUTPUT分配列名

of the first group to the second group. 第一组到第二组。 HLDGR1PUN 21454 87273 HLDGR1PUN 21454 87273

so if i wanted to have one large SQL with union ALLs, it wont work. 因此,如果我想使用一个带有联合ALL的大型SQL,它将无法正常工作。 is there something other than UNION ALL I should use? 除了UNION ALL,我还应该使用其他东西吗?

SELECT

count(*) as PROGR1PST,

(

SELECT COALESCE(SUM(OdQty#),0)

FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTDTA.OEORD1

WHERE OHCOM# = TSCOM# AND OHORD# = TSORD# AND OHCOM# = ODCOM# AND OHORD# = ODORD#

AND TSSTAT IN('AEP','BGE')

AND OHORDT IN('RTR','INT','SAM')

AND OHREQD < replace(char(current date, iso), '-', '')

AND OHHLDC = ' '

AND ODPRLC = 'ENG'

AND substr(odprt#,1,5) <> 'NOENG' AND OHORD# in(SELECT a.TSORD# FROM ASTCCDTA.TRNSTAT2 a  

WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))

) AS PROGR1PUN,

(

SELECT count(*)

FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTCCDTA.OETRA99

WHERE OHCOM# = TSCOM# AND OHORD# = TSORD#

AND (otCOM# = OHCOM# AND OTORD#= OHORD# AND ottrnc = 'AQC')

AND TSSTAT IN('AEP','BGE')

AND OHORDT IN('RTR','INT','SAM')

AND OHREQD = replace(char(current date, iso), '-', '')  AND OHHLDC = ' ' AND OHORD# in(SELECT a.TSORD# FROM ASTCCDTA.TRNSTAT2 a  

WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))

) AS PROGR1TOD,

(
etc..

UNION ALL



SELECT

count(*) as PROGR2R2PST,

(SELECT COALESCE(SUM(OdQty#),0) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTDTA.OEORD1

  WHERE OHCOM# = TSCOM# AND OHORD# = TSORD# AND OHCOM# = ODCOM# AND OHORD# = ODORD#

  AND TSSTAT IN('AEP','BGE')

  AND OHORDT IN('CUS','CIN','SMC','COC','DON')

  AND OHREQD < replace(char(current date, iso), '-', '')

  AND OHHLDC = ' '

  AND ODPRLC = 'ENG'

  AND substr(odprt#,1,5) <> 'NOENG' AND OHORD# in(SELECT a.TSORD# FROM ASTCCDTA.TRNSTAT2 a   

WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))

) AS PROGR2PUN,

I don't think you can get the result you want by "nesting" the SQL statements and joining them with UNION . 我认为您无法通过“嵌套” SQL语句并将它们与UNION来获得所需的结果。 This structure may work, depending upon your requirements: 此结构可能有效,具体取决于您的要求:

 SELECT 'Name1' AS Label , COUNT(*) AS The_Count
   FROM table1
   WHERE ...
 UNION ALL
 SELECT 'PROGR2R2PST', COUNT(*)
   FROM table2
   WHERE ...
 UNION ALL
 SELECT 'Name3', COUNT(*)
   FROM table3
   WHERE ...

This will give you back one row per select: 这将使您每次选择返回一行:

 Label      The_Count
 --------------------
 Name1          45867
 PROGR2R2PST       22
 Name3           1234

Note that the column names come from the first select . 请注意,列名称来自第一个select If this format doesn't match your requirements, please be more explicit in the question or in comments and I will try to help. 如果这种格式不符合您的要求,请在问题或注释中更明确地提出,我会尽力提供帮助。

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

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