1

My question is somewhat related to : Display multiple values of a column in one row (SQL Oracle)

However, I could not achieve desired results. Following is my Problem Statement;

I have a SQL Query ;

SELECT initiator_msisdn,  trx_type  || '/'  || SUM(trx_amt/100)  || '/'  || SUM(merchant_comm_amt/100) agent_data
FROM LBI_DM_MK.T_M_INTERNAL_AUDIT_D
WHERE DATA_DATE = '20180401'
AND trx_status  ='Completed'
GROUP BY initiator_msisdn,  trx_type
;

That returns these rows;

enter image description here

The SQL That brings this data is ;

But, I want following result.

enter image description here

Please help to sort out this issue;

khalidmehmoodawan
  • 598
  • 1
  • 5
  • 22

1 Answers1

1

You could use LISTAGG:

WITH cte AS (
  SELECT initiator_msisdn,
          trx_type  || '/'  || SUM(trx_amt/100)  || '/'  ||
          SUM(merchant_comm_amt/100) agent_data
  FROM LBI_DM_MK.T_M_INTERNAL_AUDIT_D
  WHERE DATA_DATE = '20180401'
    AND trx_status  ='Completed'
  GROUP BY initiator_msisdn,  trx_type
)
SELECT initiator_msisdn,
       LISTAGG(agent_data, '|') WITHIN GROUP (ORDER BY agent_data) AS agent_data
FROM cte
GROUP BY initiator_msisdn;
Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
  • I also managed to do it with same function meanwhile :) here is the result SELECT initiator_msisdn, LISTAGG(agent_data, ', ') WITHIN GROUP (ORDER BY agent_data) "grp" FROM ( SELECT initiator_msisdn, trx_type || ':' || SUM(trx_amt/100) || '/' || SUM(merchant_comm_amt/100) agent_data FROM LBI_DM_MK.T_M_INTERNAL_AUDIT_D WHERE DATA_DATE = '20180401' AND trx_status ='Completed' GROUP BY initiator_msisdn, trx_type ) GROUP BY initiator_msisdn; – khalidmehmoodawan Apr 02 '18 at 13:45