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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:28:49+00:00 2026-05-22T01:28:49+00:00

for instance if I have a table like this total date 10 2010-01-01 15

  • 0

for instance if I have a table like this

total   date
10      2010-01-01
15      2010-01-02
98      2010-01-03
50      2010-01-05

how can I write select statement to get output like this?

total   date
10      2010-01-01
15      2010-01-02
98      2010-01-03
0       2010-01-04
50      2010-01-05
  • 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-22T01:28:50+00:00Added an answer on May 22, 2026 at 1:28 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. In this case, the INSERT statement needs to be run at least 31 times.

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

      SELECT x.dt
        FROM (SELECT DATE(DATE_ADD('2010-01-01', INTERVAL (n.id - 1) DAY)) AS dt
                FROM numbers n
               WHERE DATE_ADD('2010-01-01', INTERVAL (n.id - 1) DAY) <= '2010-01-05' ) x
      
    4. Use an OUTER JOIN to get your desired output:

         SELECT x.dt,
                COUNT(*) AS total
           FROM (SELECT DATE(DATE_ADD('2010-01-01', INTERVAL (n.id - 1) DAY)) AS dt
                   FROM numbers n
                  WHERE DATE_ADD('2010-01-01', INTERVAL (n.id - 1) DAY) <= '2010-01-05' ) x
      LEFT JOIN YOUR_TABLE y ON y.date = x.dt
       GROUP BY x.dt
       ORDER BY x.dt
      

    Why Numbers, not Dates?

    Simple – dates can be generated based on the number, like in the example I provided. It also means using a single table, vs say one per data type.

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

Sidebar

Related Questions

I have a table in a db that has a structure like this: Date
For instance I have a string like this : abc123[*]xyz[#]098[~]f9e [*] , [#] and
I have a table like this: +-------+---------+------+-----+---------+-------+ | Field | Type | Null |
I have a table like this: <table> <tr> <th>Header</th> <td>Content</td> </tr> <tr> <th>Header</th> <td>Content</td>
If I have the name of a database table like this: string tableName =
In the simple case, assume I have a table that looks like this: mysql>
I have a table like this... ID Description LowThreshold HighThreshold 1 BAD 0.0 .69
I am using SQL server 2005. I have a table like this - ID
I have an instance of System.Type, for which IsArray returns true. How can I
I have a tsql snippet similar to this: select p.Make, count(p.Make) as _count from

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.