将 DB2 中的 2 个 Select Queries 合并为一个 Result

Posted

技术标签:

【中文标题】将 DB2 中的 2 个 Select Queries 合并为一个 Result【英文标题】:Combine 2 Select Queries from DB2 into one Result 【发布时间】:2014-09-29 14:38:07 【问题描述】:

我的 DB2 数据库有两个查询:

SELECT XEDCDTA.DCADR.ADRNAME as Name, 
XEDCDTA.DCADR.ADRCITY as City, 
XEDCDTA.DCDDT.DDTGRMA as Gross,
XEDCDTA.DCDDT.DDTCOCD as TN,
XEDCDTA.DCDDT.DDTDES as Description,
XEDCDTA.DCDDT.DDTVATVA as VATVALUE,
XEDCDTA.DCDHD.DHDTRN as TRUCK,
XEDCDTA.DCDHD.DHDTRE as REFEENCE,
FROM XEDCDTA.DCDHD, XEDCDTA.DCDDT, XEDCDTA.DCADR
WHERE XEDCDTA.DCDHD.DHDTRE LIKE ('123456') AND 
XEDCDTA.DCADR.ADRTYPE LIKE ('ABC') AND
XEDCDTA.DCDDT.DHDKEY = XEDCDTA.DCDHD.DHDKEY AND
XEDCDTA.DCADR.DHDKEY = XEDCDTA.DCDHD.DHDKEY 

和第二个查询

SELECT
XEDCDTA.DCADR.ADRREF as Reference2
FROM XEDCDTA.DCDHD, XEDCDTA.DCDDT, XEDCDTA.DCADR
WHERE XEDCDTA.DCDHD.DHDTRE LIKE ('123456') AND 
XEDCDTA.DCADR.ADRTYPE LIKE ('XYZ') AND
XEDCDTA.DCDDT.DHDKEY = XEDCDTA.DCDHD.DHDKEY AND
XEDCDTA.DCADR.DHDKEY = XEDCDTA.DCDHD.DHDKEY

我试过了:

SELECT XEDCDTA.DCADR.ADRNAME as Name, 
XEDCDTA.DCADR.ADRCITY as City, 
XEDCDTA.DCDDT.DDTGRMA as Gross,
XEDCDTA.DCDDT.DDTCOCD as TN,
XEDCDTA.DCDDT.DDTDES as Description,
XEDCDTA.DCDDT.DDTVATVA as VATValue,
XEDCDTA.DCDHD.DHDTRN as Truck,
XEDCDTA.DCDHD.DHDTRE as Reference1,
cast(NULL as varchar(128)) as Reference2
FROM XEDCDTA.DCDHD, XEDCDTA.DCDDT, XEDCDTA.DCADR
WHERE XEDCDTA.DCDHD.DHDTRE LIKE ('123456') AND 
XEDCDTA.DCADR.ADRTYPE LIKE ('ABC') AND
XEDCDTA.DCDDT.DHDKEY = XEDCDTA.DCDHD.DHDKEY AND
XEDCDTA.DCADR.DHDKEY = XEDCDTA.DCDHD.DHDKEY 
UNION ALL
SELECT cast(NULL as varchar(128)) as Name, 
cast(NULL as varchar(128)) as City, 
cast(NULL as varchar(128)) as Gross,
cast(NULL as varchar(128)) as TN,
cast(NULL as varchar(128)) as Description,
cast(NULL as varchar(128)) as VATValue,
cast(NULL as varchar(128)) as Truck,
cast(NULL as varchar(128)) as Reference1,
XEDCDTA.DCADR.ADRREF as Reference2
FROM XEDCDTA.DCDHD, XEDCDTA.DCDDT, XEDCDTA.DCADR
WHERE XEDCDTA.DCDHD.DHDTRE LIKE ('123456') AND 
XEDCDTA.DCADR.ADRTYPE LIKE ('XYZ') AND
XEDCDTA.DCDDT.DHDKEY = XEDCDTA.DCDHD.DHDKEY AND
XEDCDTA.DCADR.DHDKEY = XEDCDTA.DCDHD.DHDKEY ) s

但是从这个查询中,结果行填充了“NULL”。只有列 Reference2 已填充查询中的值。

结果应该是这样的:

名称、城市、总收入、TN、描述、VATValue、卡车、参考、参考2

DHDKEY 是所有表的主键。

谁能给我一个提示?

我也尝试了一些适用于 mysql 的语句,但我在 DB2 中遇到了异常。

感谢您的帮助。

干杯克劳迪奥

【问题讨论】:

【参考方案1】:

试试这个:

SELECT 
XEDCDTA.DCDHD.DHDKEY AS DHDKEY,
XEDCDTA.DCADR.ADRTYPE AS ADRTYPE,
XEDCDTA.DCADR.ADRNAME AS Name, 
XEDCDTA.DCADR.ADRCITY AS City, 
XEDCDTA.DCDDT.DDTGRMA AS Gross,
XEDCDTA.DCDDT.DDTCOCD AS TN,
XEDCDTA.DCDDT.DDTDES AS Description,
XEDCDTA.DCDDT.DDTVATVA AS VATValue,
XEDCDTA.DCDHD.DHDTRN AS Truck,
CASE WHEN XEDCDTA.DCADR.ADRTYPE = 'ABC' THEN XEDCDTA.DCDHD.DHDTRE ELSE NULL END AS Reference1,
CASE WHEN XEDCDTA.DCADR.ADRTYPE = 'XYZ' THEN XEDCDTA.DCADR.ADRREF ELSE NULL END AS Reference2
FROM XEDCDTA.DCDHD
INNER JOIN XEDCDTA.DCDDT ON XEDCDTA.DCDDT.DHDKEY = XEDCDTA.DCDHD.DHDKEY
INNER JOIN XEDCDTA.DCADR ON XEDCDTA.DCADR.DHDKEY = XEDCDTA.DCDHD.DHDKEY 
WHERE XEDCDTA.DCDHD.DHDTRE = ('123456')
AND XEDCDTA.DCADR.ADRTYPE IN ('ABC','XYZ') 

【讨论】:

以上是关于将 DB2 中的 2 个 Select Queries 合并为一个 Result的主要内容,如果未能解决你的问题,请参考以下文章

在 db2 中合并两个 select 语句的结果

DB2 SELECT COUNT,如果为 NULL,则默认为 0

DB2 SQL:如何将“WHERE EXISTS”表中的列添加到选择语句

出现错误 在执行 DB2 EXPORT IXF 时处理 Action String 参数中的 SELECT 字符串时发生 SQL 错误“-204”?

DB2的 部分SQL语句

DB2 中的 ROW_NUMBER()