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

  • Home
  • SEARCH
  • 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 6194533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:18:43+00:00 2026-05-24T03:18:43+00:00

My MySQL database has the following column headings: month, typeOfWork, totalHours I am interested

  • 0

My MySQL database has the following column headings:

month, typeOfWork, totalHours

I am interested in displaying the results according to typeOfWork and month. For example, all typeOfWork entries will be displayed in the first column on the left, and each field will total that particular typeOfWork in each column’s specified month. As such, my SQL pivot table statement is currently:

SELECT
IFNULL(typeOfWork, 'Total') AS TypeOfWork,
SUM(IF(month='JAN',totalHours,NULL)) AS JAN,    
SUM(IF(month='FEB',totalHours,NULL)) AS FEB,    
SUM(IF(month='MAR',totalHours,NULL)) AS MAR,    
SUM(IF(month='APR',totalHours,NULL)) AS APR,    
SUM(IF(month='MAY',totalHours,NULL)) AS MAY,    
SUM(IF(month='JUN',totalHours,NULL)) AS JUN,    
SUM(IF(month='JUL',totalHours,NULL)) AS JUL,    
SUM(IF(month='AUG',totalHours,NULL)) AS AUG,
SUM(IF(month='SEP',totalHours,NULL)) AS SEP,    
SUM(IF(month='OCT',totalHours,NULL)) AS OCT,    
SUM(IF(month='NOV',totalHours,NULL)) AS NOV,    
SUM(IF(month='DEC',totalHours,NULL)) AS DEC

FROM $databasetable GROUP BY typeOfWork
WITH ROLLUP

I’m currently getting a syntax error specifying the last line (from ‘DEC through to ROLLUP’). My confusion comes from the fact that I have simply adapted this table from another working pivot table in my script. I think the difference might be in my two requirements (sum totalHours where month equals column name AND typeOfWork equals typeOfWork in first column). I’m wondering if my IF statements require an AND clause?

Can anybody point me in the right direction with this pivot table? It would be greatly appreciated. Thank you!

  • 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-24T03:18:43+00:00Added an answer on May 24, 2026 at 3:18 am

    Your query is correct.
    Only one thing – DEC is a MySQL reserved word, this word cannot be used as object identifier directly; so, just quote it with backticks –

    SELECT
    IFNULL(typeOfWork, 'Total') AS TypeOfWork,
    SUM(IF(month='JAN',totalHours,NULL)) AS JAN,    
    ...
    SUM(IF(month='DEC',totalHours,NULL)) AS `DEC`
    
    FROM $databasetable GROUP BY typeOfWork
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an table in my MySql database that has the following columns: -
Say I have a mysql table of names and one column has the following
I have a following scenario in my SQL Server 2005 database. zipcodes table has
My MySQL database has date of birth stored in the format yyyy-mm-dd, how do
I read data from a mysql database that has is filled by php scripts.
I have created a MySQL database using MySQL Workbench. It has about 20 tables.
I've got a MySQL database that stores image information. Right now it only has
Data that is stored in my mysql database, has the breaks of text input
I have a website that has approx 1000 different strings in a mysql database
We need to redo a database in MySQL that has been already done on

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.