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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:54:22+00:00 2026-05-28T22:54:22+00:00

I’m stumped by pivoting a table and the frustration level is rising. I realize

  • 0

I’m stumped by pivoting a table and the frustration level is rising. I realize the answer is probably in front of me but I can’t find it. I’ve tried case statements, group_concat, subqueries all to no avail.

How to get the current table…

super_id    cat_id  qa  qb
   1           1    5   5
   1           2    2   5
   1           3    3   4
   2           4    5   3
   2           5    3   4
   2           6    4   2

to look like this…

       1     2 <--- super_id
qa    3.33  4.00 <--- avg
qb    4.67  3.00

Thanks!

  • 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-05-28T22:54:22+00:00Added an answer on May 28, 2026 at 10:54 pm

    As already mentioned, MySQL isn’t built to pivot on its own and you should probably pivot in code if possible, but barring that, below is some example SQL that should do what you want. Assuming your actual table is variable in some sense (like how many q[x] columns you have, or how many super_id’s you have), you can wrap this up in a stored procedure to dynamically generate and execute the necessary SQL.

    Assuming an initial table named testTable:

    mysql> select * from testTable;
    +----------+--------+------+------+
    | super_id | cat_id | qa   | qb   |
    +----------+--------+------+------+
    |        1 |      1 |    5 |    5 | 
    |        1 |      2 |    2 |    5 | 
    |        1 |      3 |    3 |    4 | 
    |        2 |      4 |    5 |    3 | 
    |        2 |      5 |    3 |    4 | 
    |        2 |      6 |    4 |    2 | 
    +----------+--------+------+------+
    6 rows in set (0.00 sec)
    

    Use the following SQL:

    SELECT
        'qa' q,
        SUM( CASE WHEN super_id = 1 THEN qa ELSE 0 END )/SUM( CASE WHEN super_id = 1 THEN 1 ELSE 0 END ) super_id_1,
        SUM( CASE WHEN super_id = 2 THEN qa ELSE 0 END )/SUM( CASE WHEN super_id = 2 THEN 1 ELSE 0 END ) super_id_2
    FROM
        testTable
    UNION ALL
    SELECT
        'qb' q,
        SUM( CASE WHEN super_id = 1 THEN qb ELSE 0 END )/SUM( CASE WHEN super_id = 1 THEN 1 ELSE 0 END ) super_id_1,
        SUM( CASE WHEN super_id = 2 THEN qb ELSE 0 END )/SUM( CASE WHEN super_id = 2 THEN 1 ELSE 0 END ) super_id_2
    FROM
        testTable
    ;
    

    To achieve this result:

    +----+------------+------------+
    | q  | super_id_1 | super_id_2 |
    +----+------------+------------+
    | qa |     3.3333 |     4.0000 | 
    | qb |     4.6667 |     3.0000 | 
    +----+------------+------------+
    2 rows in set (0.00 sec)
    

    (edit: formatting)

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

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
Does anyone know how can I replace this 2 symbol below from the string

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.