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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:58:36+00:00 2026-06-13T17:58:36+00:00

I have a database with two tables. The first (‘calendar’) contains only a series

  • 0

I have a database with two tables. The first (‘calendar’) contains only a series of dates like ‘2012-12-25’ into the future. This was generated by a procedure. The second (‘new_allocations’) contains a series of stock allocations. Sample content looks like this:

+----+-------------+------------+----------+------------+
| id | delivery_ID | date       | quantity | product_ID |
+----+-------------+------------+----------+------------+
|  1 |           1 | 2012-11-09 |        5 |          2 |
|  2 |           1 | 2012-11-08 |        5 |          2 |
|  3 |           2 | 2012-11-07 |        5 |          2 |
|  4 |           3 | 2012-11-06 |        5 |          2 |
|  5 |           4 | 2012-11-03 |        2 |          2 |
|  6 |           4 | 2012-11-02 |        5 |          2 |
|  7 |           5 | 2012-11-03 |        3 |          2 |
|  8 |           6 | 2012-11-05 |        5 |          2 |
|  9 |           7 | 2012-11-07 |       55 |          5 |
| 10 |           7 | 2012-11-06 |       34 |          5 |
| 11 |           7 | 2012-11-05 |       40 |          5 |
+----+-------------+------------+----------+------------+

The following query (which basically means ‘show me a list of days in the date range supplied, excluding Sundays, with a total of stock allocations for that day’) is almost what I want, except that it is for all products (denoted by ‘product_ID’).

select datefield as Date, sum(ifnull(quantity,0)) as Qty from calendar left join new_allocations on (new_allocations.date=calendar.datefield) where (calendar.datefield>='2012-10-29' and calendar.datefield<='2012-11-10') and dayname(calendar.datefield) != 'Sunday' group by datefield;

+------------+------+
| Date       | Qty  |
+------------+------+
| 2012-10-29 |    0 |
| 2012-10-30 |    0 |
| 2012-10-31 |    0 |
| 2012-11-01 |    0 |
| 2012-11-02 |    5 |
| 2012-11-03 |    5 |
| 2012-11-05 |   45 |
| 2012-11-06 |   39 |
| 2012-11-07 |   60 |
| 2012-11-08 |    5 |
| 2012-11-09 |    5 |
| 2012-11-10 |    0 |
+------------+------+

So my problem is that as soon as I add “and product_ID=’2′” the query stops returning all dates in the range:

New query:

select datefield as Date, sum(ifnull(quantity,0)) as Qty from calendar left join new_allocations on (new_allocations.date=calendar.datefield) where (calendar.datefield>='2012-10-29' and calendar.datefield<='2012-11-10') and dayname(calendar.datefield) != 'Sunday' and product_ID='2' group by datefield;

New result:

+------------+------+
| Date       | Qty  |
+------------+------+
| 2012-11-02 |    5 |
| 2012-11-03 |    5 |
| 2012-11-05 |    5 |
| 2012-11-06 |    5 |
| 2012-11-07 |    5 |
| 2012-11-08 |    5 |
| 2012-11-09 |    5 |
+------------+------+

In fact what I want looks like this:

+------------+------+
| Date       | Qty  |
+------------+------+
| 2012-10-29 |    0 |
| 2012-10-30 |    0 |
| 2012-10-31 |    0 |
| 2012-11-01 |    0 |
| 2012-11-02 |    5 |
| 2012-11-03 |    5 |
| 2012-11-05 |    5 |
| 2012-11-06 |    5 |
| 2012-11-07 |    5 |
| 2012-11-08 |    5 |
| 2012-11-09 |    5 |
| 2012-11-10 |    0 |
+------------+------+

I have been backwards and forwards through this and have not been able to phrase the query correctly – what am I missing?

Many thanks in advance.

CREATE TABLE `new_allocations` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `delivery_ID` int(11) DEFAULT NULL,
  `date` date DEFAULT NULL,
  `quantity` int(11) DEFAULT NULL,
  `product_ID` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
)

INSERT INTO `new_allocations` (`id`, `delivery_ID`, `date`, `quantity`, `product_ID`)
VALUES
    (1,1,'2012-11-09',5,2),
    (2,1,'2012-11-08',5,2),
    (3,2,'2012-11-07',5,2),
    (4,3,'2012-11-06',5,2),
    (5,4,'2012-11-03',2,2),
    (6,4,'2012-11-02',5,2),
    (7,5,'2012-11-03',3,2),
    (8,6,'2012-11-05',5,2),
    (9,7,'2012-11-07',55,5),
    (10,7,'2012-11-06',34,5),
    (11,7,'2012-11-05',40,5);

For the calendar:

DROP TABLE IF EXISTS `calendar`;
CREATE TABLE `calendar` (
  `datefield` date DEFAULT NULL
)

The procedure to fill the calendar:

DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `fill_calendar`(start_date DATE, end_date DATE)
BEGIN
  DECLARE crt_date DATE;
  SET crt_date=start_date;
  WHILE crt_date < end_date DO
    INSERT INTO calendar VALUES(crt_date);
    SET crt_date = ADDDATE(crt_date, INTERVAL 1 DAY);
  END WHILE;
END;;
DELIMITER ;
  • 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-13T17:58:37+00:00Added an answer on June 13, 2026 at 5:58 pm

    Add your condition into the ON clause, not the WHERE clause:

    SELECT  datefield AS Date, COALESCE(SUM(quantity), 0) AS Qty
    FROM    calendar
    LEFT JOIN
            new_allocations
    ON      new_allocations.date = calendar.datefield
            AND product_ID = '2'
    WHERE   calendar.datefield BETWEEN '2012-10-29' AND '2012-11-10'
            AND dayname(calendar.datefield) != 'Sunday'
    GROUP BY
            datefield
    

    WHERE filters the results of the join. If there were no allocations for a given date, the left join will yield one record for the date with all new_allocations fields (including productId) set to NULL.

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

Sidebar

Related Questions

I have a database framework where I have two tables. The first table has
I have a database with two tables, tblPlayers and tblMatches. tblPlayers contains the following
I have a database that which contains two tables, links and tour. Tour is
I have SQL Server 2008 database with two tables. The first table is called
I have a SQL Server 2008 database with two tables. The first table is
I have a database that has two tables, one of which contains a foreign
I have two tables in my database. The first one, [nodeActivity] has the following
I have two tables in my SQL Server database. The first is Test1 and
I have a database with two tables. One of the tables contains users, the
I have an h2 database with two tables with foreign keys like CREATE TABLE

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.