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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:16:11+00:00 2026-05-24T02:16:11+00:00

Using the MySQL query below, I have created a pivot table which is pretty

  • 0

Using the MySQL query below, I have created a pivot table which is pretty much exactly what I am looking for. I would however like to replace the NULL values with actual descriptions, such as SubTotal and GrandTotal. Here is the pivot table format as displayed on my PHP output (hopefully the formatting is somewhat legible!).

Name    Division 1  Division 2  Division 3  Division 4  Location
Name 1
Name 2
Name 3
NULL    Total       Total       Total       Total
Name 4
Name 5
NULL    Total       etc
NULL    Column Grand Total 

Here is the query I used to generate the table. After researching this issue, it seems like the CASE function is the way to go. However, when I add the two CASE lines into the query below, it doesn’t seem to want to work. The mysql_error that returns says that the “GROUPING function does not exist”.

SELECT 
CASE WHEN (GROUPING(name)=1) THEN 'MainTotal' ELSE name END AS name, 
CASE WHEN (GROUPING(location)=1) THEN 'SubTotal' ELSE location END AS location, 
name AS Name,
SUM(IF(division='OEM',totalHours,NULL)) AS OEM,
SUM(IF(division='A/M',totalHours,NULL)) AS AM,
SUM(IF(division='SKF',totalHours,NULL)) AS SKF,
SUM(IF(division='RE',totalHours,NULL)) AS RE,
location as Location
FROM $databasetable GROUP BY location, name 
WITH ROLLUP

Can anybody tell me what I’m doing wrong? Is the CASE function the way to go to replace the NULL category titles?

Thanks in advance!

  • 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-24T02:16:12+00:00Added an answer on May 24, 2026 at 2:16 am

    Try it like this:

    SELECT 
    IFNULL(name, 'MainTotal') AS name, 
    IFNULL(location, 'SubTotal') AS location, 
    SUM(IF(division='OEM',totalHours,NULL)) AS OEM,
    SUM(IF(division='A/M',totalHours,NULL)) AS AM,
    SUM(IF(division='SKF',totalHours,NULL)) AS SKF,
    SUM(IF(division='RE',totalHours,NULL)) AS RE,
    location as Location
    FROM $databasetable GROUP BY location, name 
    WITH ROLLUP
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL query below that I'm using to get events for my
I have been using the MySql Query Browser and [opinion]it has to be the
Using MySQL syntax, how would I write a query to return the following (I
I have a dataset in MySQL where using limit is already an expensive query,
Note: Using MySQL 4.0, which means no subqueries (at present). I have 2 tables:
So I setup a table using mysql query browser, and now i'm trying to
I have created a MySQL table where one of the columns stores a BLOB
I'm using the MySQL Query Browser (part of the MySQL GUI Tools ) and
Using standard mysql functions is there a way to write a query that will
Using MySQL syntax and having a table with a row like: mydate DATETIME NULL,

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.