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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:32:47+00:00 2026-06-18T04:32:47+00:00

I am trying to summarize records using the following query in MySQL. It works

  • 0

I am trying to summarize records using the following query in MySQL. It works great as long as there is at least one record in each year. If records are missing in years, then the year doesn’t show up. How can I modify this to show each year within my filter?

SELECT SUM( SICK_SIZE + DEAD_SIZE ) AS Cases, DATE_FORMAT( EVENT_DATE,  '%Y' ) AS DateYear
FROM report_case_ext
WHERE DATE_FORMAT( EVENT_DATE,  '%Y' ) >= DATE_FORMAT( DATE_ADD( CURDATE( ) , INTERVAL -4YEAR ) ,  '%Y' ) 
AND DATE_FORMAT( EVENT_DATE,  '%Y' ) <= DATE_FORMAT( CURDATE( ) ,  '%Y' ) 
GROUP BY DATE_FORMAT( EVENT_DATE,  '%Y' ) 
  • 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-18T04:32:48+00:00Added an answer on June 18, 2026 at 4:32 am

    In MySQL, you can use sqlvariables, join to any other table to simulate row creation — which returns a valid result set of the years you are looking for, then LEFT-JOIN to your other table so you know you’ll always get the years you want…

    select
          YearsYouWant.RequireYear as DateYear,
          SUM( RCE.SICK_SIZE + RCE.DEAD_SIZE ) AS Cases
       from
          ( select @nYear := @nYear +1 as RequireYear
               from report_case_ext,
                    ( select @nYear := year( curdate()) -5 ) sqlvars
               limit 5 ) as YearsYouWant
             LEFT JOIN
                report_case_ext RCE
                   on YearsYouWant.RequireYear = year( RCE.Event_Date )
       GROUP BY 
          YearsYouWant.RequireYear
    

    The inner prequery that uses “report_case_ext” is only used to have a table of at least 5 records to keep the years you want… In this case,

    @nYear is initialized to 1 year less than the 4 you were looking for — hence -5

    curdate() = 2013 – 5 = 2008.

    Then, in the select @nYear := @nYear +1 first time will have the first year become 2009 and complete for 5 years, thus generating a record for 2009, 2010, 2011, 2012 and 2013 (via LIMIT 5)

    Now that result (of all years) is LEFT-joined to the report_case_ext table on common years. So, even those that have no dates

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

Sidebar

Related Questions

I am trying to summarize the output of a query but am struggling. The
I'm trying to summarize data from several Google spreadsheet' sheets in a single one
I'm trying to use data.table in R to summarize the following data table: SiteNo
I'm trying to run a simple query with node-mysql like this: client.query(UPDATE mytable SET
I keep trying to ask this question but then my code's to long etc
What I am trying to do is: if one or more checkboxes are selected,
I am trying to calculate the area of a graph using integrals. The user
Just to summarize, I'm trying to make a live page render (browse websites) while
I'm trying to authenticate to an API, which only allows you to authenticate using
Hard to summarize in one question, but here are the details: I have an

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.