SELECT
T2.MERGE_ID
,T1.A
,CASE WHEN T1.A=T2.CUR_NAME THEN T2.AMT ELSE 0 END A_AMT
,T1.B
,CASE WHEN T1.B=T2.CUR_NAME THEN T2.AMT ELSE 0 END B_AMT
,T1.C
,CASE WHEN T1.C=T2.CUR_NAME THEN T2.AMT ELSE 0 END C_AMT
FROM
(SELECT 'USD' A,'CNY' B,'EUR' C FROM DUAL) T1 LEFT JOIN
(
SELECT DMS.`MERGE_ID`,DMS.`CUR_NAME`,SUM(DMS.`AMT`) FROM
`dms` GROUP BY DMS.`MERGE_ID`,DMS.`CUR_NAME`
) T2
ON A=T2.CUR_NAME
OR B=T2.CUR_NAME
OR C=T2.CUR_NAME