Search code examples
mysqlsqlgroup-concat

Mysql query concate data into group concate


enter image description here

Here is my table. I have made the following query and get the result:

mysql - Query:

                 SELECT   

 GROUP_CONCAT(CONCAT('', j0.rent_agree_id,'-',j0.rent_sche_id) ) AS agreement_ref_no
                FROM
                  rent_provision_history AS j0

                  GROUP BY provision_date

Result:

enter image description here

But I want to see the result differently.

For example for the last result row i am getting now: 4-68,4-69,6-107,6-108,6-109

But I want to see it like that: 4(68,69)|5(107,108,109)

How can i do that?


Solution

  • You need two levels of aggregation:

    SELECT provision_date,
           GROUP_CONCAT(rent_agree_id, '(', rsi, ')' SEPARATOR '|') agreement_ref_no
    FROM (SELECT j0.rent_agree_id, GROUP_CONCAT(j0.rent_sche_id) as rsi
          FROM rent_provision_historyj0
          GROUP BY j0.provision_date, j0.rent_agree_id
         ) j0
    GROUP BY j0.provision_date;