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 8224271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:00:36+00:00 2026-06-07T15:00:36+00:00

I have a data in my database userid module course total 8 Biophysics A

  • 0

I have a data in my database

userid    module      course     total
  8      Biophysics     A          117
  8      BioPhysics     B           74
  8      BioPhysics     C          135

I need the output like

userid      module       Courses        grade   
 8        BioPhysics    Course A: 117    250
                        Course B: 134  
                        Course C: 35
  • 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-07T15:00:38+00:00Added an answer on June 7, 2026 at 3:00 pm

    What you’re trying to accomplish is called “PIVOTing” row data into separate columns, and it is a feature that is not supported by MySQL, but what you can do is fake it if you know beforehand the range of data you want to pivot into columns.


    You have a couple of options:

    Your example has A, B, and C for course per userid -> module combination. Assuming the values in course can only be A, B, or C: you can do a conditional aggregation for each value like so:

      SELECT userid,
             module,
             MAX(CASE course WHEN 'A' THEN total END) AS A,
             MAX(CASE course WHEN 'B' THEN total END) AS B,
             MAX(CASE course WHEN 'C' THEN total END) AS C,
             SUM(total) AS grade
        FROM tbl
    GROUP BY userid, 
             module 
    

    You can add as many CASE aggregations as you need (for D, E, F, etc.). Keep in mind some userid -> module combinations may have NULL for some of the columns if, for instance, it did not contain a row with C for course.

    But if you do not know the range of values beforehand (and perhaps there could be hundreds of different values for course, you can still get all of the data onto one row, but it wouldn’t be in separate columns – it would actually be one concatenated string in a single column. This is accomplished by using GROUP_CONCAT():

      SELECT userid,
             module,
             GROUP_CONCAT(CONCAT(course, ': ', total) SEPARATOR ' / ') AS coursetotals,
             SUM(total) AS grade
        FROM tbl
    GROUP BY userid,
             module
    

    And what this would do is output something like:

    userid  |  module      |  coursetotals             |  grade
    -------------------------------------------------------------
    8       |  BioPhysics  |  A: 117 / B: 74 / C: 135  |  326
    

    Here is a SQL-Fiddle Demo for both options


    Edit: As per your comments and edited, here is the new solution:

    SELECT 
        a.userid, 
        a.module, 
        CONCAT('Course ', course, ': ', total) AS Courses, 
        b.grade
    FROM
        tbl a
    INNER JOIN
    (
        SELECT userid, module, SUM(total) AS grade
        FROM tbl 
        GROUP BY userid, module
    ) b ON a.userid = b.userid AND a.module = b.module
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have data in my database ID UserID CourseName Grade 1 8 CourseA 75
I have some data in a database that I need represented in an XML
I have data in an SQL Server 2005 database which I need to copy
I have a very basic need to get some data from the database and
I have another problem, I have a database with UserID's with data. I want
I need to query data in Tera database from SQLSERVER 2005. I have IP
I have taken char data into database into array. now i want to convert
I have taken the data from database into array now I want it to
I have live data in a database i want to tweet data in real
I have data in this format coming from a database... BUS 101S Business and

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.