Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8942775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:36:14+00:00 2026-06-15T11:36:14+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:36:15+00:00Added an answer on June 15, 2026 at 11:36 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My first post here, so i hope this is the right area. I am
this is my first post so I hope I am posting to the right
First post on here, so I hope I'm clear enough. I have a table
this is my first post here so I hope I provide all the right
This is my first time here so I hope I post this question at
This is my first post here so I hope you could help me with
First post here, so I hope it is detailed enough. While developing an Iphone
this is my first post here on stackoverflow, so I hope I'm doing this
First time post here, I hope its a valid question. I've been building a
First post here, so I hope this is enough detail. I started using freebase-python

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.