我的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中遇到了异常。
感谢您的帮助。
干杯克劳迪奥
答案 0 :(得分:0)
试试这个:
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')