Search code examples
mysqldatabasegroup-bygroup-concat

Mysql group_concat with distinct values from another row


My first post here, hope I am following the posting guidelines right. If not, please do point out.

My table structure (credit_notes) with some sample data looks like this:

index | customer_id | user_id | doc_number | doc_date   | amount | scheme_description
+-----+-------------+---------+------------+------------+--------+-------------------
1     | 8           | 2       | IABCD001   | 2012-09-08 | 1232   | XYZ Scheme
2     | 8           | 2       | IABCD001   | 2012-09-08 | 2032   | XYZ Scheme 
3     | 8           | 2       | IABCD002   | 2012-09-09 | 2556   | XYZ Scheme 
4     | 8           | 2       | IABCD003   | 2012-09-08 | 232    | XYZ Scheme 
5     | 8           | 2       | IABCD001   | 2012-09-10 | 2556   | ABC Scheme
6     | 8           | 2       | IABCD003   | 2012-09-10 | 232    | ABC Scheme 

My requirement is to extract the total amount per scheme_description & doc_date combination, with grouping. This is my current query:

SELECT 
    SUM( `amount` ) AS `totalamount`,
    GROUP_CONCAT( CONCAT( `amount` , ";" ) ORDER BY `index` DESC SEPARATOR '' ) AS `amounts`, 
    GROUP_CONCAT( CONCAT( `doc_number` , ";" ) ORDER BY `doc_number` DESC SEPARATOR '' ) AS `docno` , 
    `doc_date` AS `docdate` , 
    `scheme_description` AS `schemedesc` 
FROM (
        SELECT 
            `c`.`index` ,
            `c`.`doc_number` , 
            `c`.`doc_date` , 
            `c`.`amount` , 
            `c`.`scheme_description` 
        FROM `credit_notes` AS `c` 
        WHERE `c`.`customer_id` = 8 
        AND `c`.`user_id` LIKE 2
)t
GROUP BY `scheme_description` , `doc_date`

This query returns this structure:

totalamount | amounts        | docno                       | docdate    | schemedesc
+-----------+----------------+-----------------------------+------------+-----------
3496        | 1232;2032;232; | IABCD001;IABCD001;IABCD003; | 2012-09-08 | XYZ Scheme
2556        | 2556;          | IABCD002;                   | 2012-09-09 | XYZ Scheme
23477       | 255;23222;     | IABCD007;IABCD007;          | 2012-09-10 | ABC Scheme

But, I need a slight change to this, I want to sum the amounts column for every distinct docno, and group it in the right order. So, as per the above data, the query should yield:

totalamount | amounts        | docno                       | docdate    | schemedesc
+-----------+----------------+-----------------------------+------------+-----------
3496        | 3264;232;      | IABCD001;IABCD003;          | 2012-09-08 | XYZ Scheme
2556        | 2556;          | IABCD002;                   | 2012-09-09 | XYZ Scheme
23477       | 23477;         | IABCD007;                   | 2012-09-10 | ABC Scheme

Is this possible using group_concat and distinct and sum. Read up all I could find, but could not find anything which pointed me to the right direction.

Is this possible?


Solution

  • Try this query -

    SELECT
      SUM(amount) totalamount,
      GROUP_CONCAT(amount) amounts,
      GROUP_CONCAT(doc_number) docno,
      doc_date docdate,
      scheme_description schemedesc
    FROM (
      SELECT scheme_description, doc_date, doc_number, SUM(amount) amount
        FROM credit_notes
        GROUP BY scheme_description, doc_date, doc_number) t
    GROUP BY scheme_description, doc_date
    

    Add WHERE, ORDER BY and other clauses if needed.