我有一个查询,该查询返回要合并为1的两行数据。它返回两行,因为有两个PYMNT_TYPES
与VOUCHER
相关联。
我想添加某种逻辑,如果PYMNT_TYPE
等于“ W”,那么我希望PYMNT_GROSS_AMT
是它自己的字段,称为WITHHOLDING_AMT
,以消除第二个行。
我苦苦挣扎的部分是,每行中还有其他列具有不同的值,并且我不确定上述更改是否会消除第二行?
我关心的第二行上唯一不在第一行上的信息是PYMNT_GROSS_AMT
,它具有与第一行上唯一的值。这就是为什么我要合并行的原因。
SELECT *
FROM PS_PYMNT_VCHR_XREF
WHERE VOUCHER_ID = '00026130'
AND BUSINESS_UNIT = '50000'
编辑代码:
SELECT
T1.*
,T2.PYMNT_GROSS_AMT
FROM
PS_PYMNT_VCHR_XREF T1
LEFT JOIN
PS_PYMNT_VCHR_XREF T2 on
T2.VOUCHER_ID = T1.VOUCHER_ID AND T2.BUSINESS_UNIT = T1.BUSINESS_UNIT
AND T2.PYMNT_TYPE = 'W'
WHERE
T1.VOUCHER_ID = '00026130'
AND T1.BUSINESS_UNIT = '50000'
答案 0 :(得分:2)
自我加入似乎就是您想要的
SELECT
T1.*
,T2.PYMNT_GROSS_AMT
FROM
PS_PYMNT_VCHR_XREF T1
LEFT JOIN
PS_PYMNT_VCHR_XREF T2 on
T2.VOUCHER_ID = T1.VOUCHER_ID --or what ever the join condition should be
T2.PYMNT_TYPE ='W'
WHERE
T1.VOUCHER_ID = '00026130'
AND T1.BUSINESS_UNIT = '50000'
and T1.PYMNT_TYPE != 'W'
答案 1 :(得分:0)
通常,您可以这样做:
SELECT *,
(SELECT PYMNT_GROSS_AMT FROM PS_PYMNT_VCHR_XREF
WHERE VOUCHER_ID = p.VOUCHER_ID
AND BUSINESS_UNIT = p.BUSINESS_UNIT
AND PYMNT_TYPES = 'W') WITHHOLDING_AMT
FROM PS_PYMNT_VCHR_XREF p
WHERE PYMNT_TYPES = 'R'
--optional conditions
AND VOUCHER_ID = '00026130'
AND BUSINESS_UNIT = '50000'