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 ;
Add your condition into the
ONclause, not theWHEREclause:WHEREfilters 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 allnew_allocationsfields (includingproductId) set toNULL.