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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:33:03+00:00 2026-05-16T07:33:03+00:00

I have two tables: CREATE TABLE ‘sales_sheet’ ( `_id` int(11) NOT NULL AUTO_INCREMENT, `_typed_by`

  • 0

I have two tables:

CREATE TABLE 'sales_sheet' (
  `_id` int(11) NOT NULL AUTO_INCREMENT,
  `_typed_by` int(11) DEFAULT NULL,
  `_product_id` int(11) DEFAULT NULL,
  `_year` date DEFAULT NULL,
  `_validation_state` int(11) DEFAULT NULL,
  PRIMARY KEY  (`_id`),
  KEY `_product_id` (`_product_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

CREATE TABLE 'sales_sheet_entries' (
  `_id` int(11) NOT NULL AUTO_INCREMENT,
  `_sheet_id` int(11) DEFAULT NULL,
  `_month` date DEFAULT NULL,
  `_quantity` int(11) DEFAULT NULL,
  PRIMARY KEY  (`_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

These two tables are used to store sheets of annual sales. The first table stores the product identifier, the year and the validation status of the sheet. The second stores the monthly sales for a product.

I want, in a single query get this result :

---------------------------------------------
| Month         | Sales volume  |
---------------------------------------------
| January 2010      |       |
| February 2010     | XXXXXX    |
| March 2010        | XXXXXX    |
| April 2010        | XXXXXX    |
| May 2010          |       |
| June 2010         |       |
| July 2010         |       |
| August 2010       |       |
| September 2010        |       |
| October 2010      | XXXXXX    |
| November 2010     | XXXXXX    |
| December 2010     |       |
---------------------------------------------

Empty fields in the sales volume column may correspond to a record already saved in the sales_sheet_entries table but has no corresponding sales volume or simply non-existent on the database, but the query must display it.

One of my constraints is that I can not put 12 columns correspond directly to the list of months in the sales_sheet table because the client specifications are changing and he may demand that sales volume will be filled by period instead of month.

I hope I was clear enough, thank you in advance for your help & sorry for my english.

  • 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-16T07:33:04+00:00Added an answer on May 16, 2026 at 7:33 am

    MySQL doesn’t have recursive functionality, so you’re left with using the NUMBERS table trick –

    1. Create a table that only holds incrementing numbers – easy to do using an auto_increment:

      DROP TABLE IF EXISTS `example`.`numbers`;
      CREATE TABLE  `example`.`numbers` (
        `id` int(10) unsigned NOT NULL auto_increment,
         PRIMARY KEY  (`id`)
      ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
      
    2. Populate the table using:

      INSERT INTO NUMBERS
        (id)
      VALUES
        (NULL)
      

      …for as many values as you need.

    3. Use DATE_ADD to construct a list of times, increasing the months based on the NUMBERS.id value:

      SELECT x.*
        FROM (SELECT DATE_ADD('2010-01-01', INTERVAL n.id - 1 MONTH)
                FROM numbers n) x
      
    4. LEFT JOIN onto your table of data based on the datetime portion:

         SELECT DATE_FORMAT(x.dt, '%M %Y') AS Month,
                y.sales_volume
           FROM (SELECT DATE_ADD('2010-01-01', INTERVAL n.id - 1 MONTH) AS dt
                   FROM numbers n) x
      LEFT JOIN (SELECT ss._product_id,
                        ss._year,
                        sse._month,
                        SUM(sse._quantity) AS sales_volume
                   FROM SALES_SHEET ss
                   JOIN SALES_SHEET_ENTRIES sse ON sse._sheet_id = ss._id
                GROUP BY ss._product_id, ss._year, sse._month) y ON y._month = MONTH(x.dt)
                                                                AND y._year = YEAR(x.dt)
                                                                AND y._product_id = ?
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.