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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:44:47+00:00 2026-06-13T14:44:47+00:00

I have a table with fields like this: Item Category Year Month Value I

  • 0

I have a table with fields like this:

Item
Category
Year 
Month
Value

I want to aggregate the result from this table to display sum(value) for each category, year and month. So I have this query:

SELECT Category, Year, Month, SUM(Value) 
FROM Table
GROUP BY Category, Year, Month;

This works fine, but for months in which the sum is zero, the month is not displayed. I want all months to be displayed with zero for the total value where applicable.

I’ve tried doing COALESCE(SUM(Value),0), but this didn’t work.

I’ve tried creating a separate table (Table2) with the year and month values and then joining this with the first table:

SELECT Category, Year, Month, SUM(Value) 
FROM Table
RIGHT JOIN Table2
ON Table2.Year + Table2.Month = Table.Year + Table.Month
GROUP BY Category, Year, Month;

This doesn’t work either. What am I doing wrong?

Edit:

I have tried Dems’s solution, using the following query:

SELECT
  Category.name,
  Dates.Year,
  Dates.Month,
  SUM(Data.Value) AS Value
FROM
  Category
CROSS JOIN
  Dates
LEFT JOIN
  Data
    ON  data.CategoryID = Category.ID
    AND data.Year       = Dates.Year
    AND data.Month      = Dates.Month
WHERE data.Year BETWEEN #### and ####
  AND data.Month BETWEEN # AND #
  AND data.Property IN (‘A1’,’B1’) 
  AND data.Property2 IN (‘C1’,’D1’)
GROUP BY
  Category.name,
  Dates.Year,
  Dates.Month

The only differences as far as can see are that I’m using a where clause on data, and have missed of the ORDER BY, which shouldn’t make a difference. But I’m still having no joy.

  • 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-13T14:44:49+00:00Added an answer on June 13, 2026 at 2:44 pm

    You need to add Category to your Table2, or (even better) have a Category table.

    Then, you need to group by those lookup tables, not the data table.

    SELECT
      Category.name,
      Dates.Year,
      Dates.Month,
      SUM(Data.Value) AS Value
    FROM
      Category
    CROSS JOIN
      Dates
    LEFT JOIN
      Data
        ON  data.CategoryID = Category.ID
        AND data.Year       = Dates.Year
        AND data.Month      = Dates.Month
        AND data.Property  IN ('A1','B1') 
        AND data.Property2 IN ('C1','D1')
    WHERE Dates.Year  BETWEEN #### AND ####
      AND Dates.Month BETWEEN #### AND ####
    GROUP BY
      Category.name,
      Dates.Year,
      Dates.Month
    ORDER BY
      Category.name,
      Dates.Year,
      Dates.Month
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have my Table structure like this :: ATT_Table : Fields - Act_ID, Assigned_To_ID,
I have a table like this, with three fields: User | Question# | Answer
I have a select like this: SELECT field1, field2, field3 FROM table WHERE field1=
I have a table like this : +-------+------------+------+-----+---------+-------+ | Field | Type | Null
I have a MySQL table looking like this: > describe books; +---------------+--------------+------+-----+---------+----------------+ | Field
I have a sql like this: SELECT TOP 1 field_name * FROM table_name and
I have a favorite table containing some fields like login_id,driver_id(One login_id may have many
I have a user table 'users' that has fields like: id first_name last_name ...
I have a table containing a bunch of input fields, sort of like a
I have inherited a table with a structure something like this: ID Name Timestamp

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.