Reputation: 1348
DROP TABLE IF EXISTS `items_stock`;
CREATE TABLE IF NOT EXISTS `items_stock` (
`ItemStockID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`ItemID` mediumint(8) unsigned NOT NULL,
`Date` date NOT NULL,
`CurrentStock` decimal(14,2) NOT NULL DEFAULT '0.00',
`CreatedBy` int(5) NOT NULL,
`CreatedDate` datetime NOT NULL,
`ModifiedBy` int(5) DEFAULT NULL,
`ModifiedDate` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
`Active` enum('1','0') NOT NULL DEFAULT '1',
PRIMARY KEY (`ItemStockID`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
INSERT INTO `items_stock` (`ItemStockID`, `ItemID`, `Date`, `CurrentStock`, `CreatedBy`, `CreatedDate`, `ModifiedBy`, `ModifiedDate`, `Active`) VALUES
(1, 1, '2010-11-03', 500.00, 1, '2010-11-03 11:12:12', NULL, '2010-11-04 09:39:29', '1'),
(2, 1, '2010-11-04', 400.00, 1, '2010-11-04 11:12:12', NULL, '2010-11-04 10:10:09', '1'),
(3, 1, '2010-11-04', 700.00, 1, '2010-11-04 11:14:12', NULL, '0000-00-00 00:00:00', '1'),
(4, 1, '2010-11-03', 600.00, 1, '2010-11-04 11:19:12', NULL, '2010-11-04 10:11:26', '1'),
(5, 2, '2010-11-05', 800.00, 1, '2010-11-05 11:19:12', NULL, '2010-11-05 10:11:26', '1');
Now i want to get the current stock of all the items for today(2010-11-05), for example Item with ID 1 current stock is 700 (ItemStockID is 3)
I tried with following query
SELECT MAX(DATE) AS DATE, ItemStockID, ItemID, CurrentStock FROM items_stock WHERE DATE <= '2010-11-05' AND Active = 1 GROUP BY ItemID;
+------------+-------------+--------+--------------+
| DATE | ItemStockID | ItemID | CurrentStock |
+------------+-------------+--------+--------------+
| 2010-11-04 | 1 | 1 | 500.00 |
| 2010-11-05 | 5 | 2 | 800.00 |
+------------+-------------+--------+--------------+
But it was not giving proper current stock if same date is there more than one time
Actual result should be like this
+------------+-------------+--------+--------------+
| DATE | ItemStockID | ItemID | CurrentStock |
+------------+-------------+--------+--------------+
| 2010-11-04 | 3 | 1 | 700.00 |
| 2010-11-05 | 5 | 2 | 800.00 |
+------------+-------------+--------+--------------+
If i use following query it was giving above result
SELECT ISTI.ItemStockID, ISTI.DATE, ISTI.ItemID, ISTI.CurrentStock FROM items_stock AS ISTI
JOIN (SELECT MAX(ItemStockID) AS ItemStockID
FROM items_stock AS IST
JOIN (SELECT MAX(DATE) AS DATE, ItemID FROM items_stock WHERE DATE <= '2010-11-05' AND Active = 1 GROUP BY ItemID) AS SD ON SD.Date = IST.Date AND IST.ItemID = SD.ItemID
GROUP BY IST.ItemID
) AS ISTO ON ISTO.ItemStockID = ISTI.ItemStockID;
Can you tell me how to get the above results in optimized way
Upvotes: 1
Views: 973
Reputation: 6423
Well, I think mysql query is behaving as it should. you need a different query. your original query would return the first row in the group that matches your group. But, you want the opposite. I think you want the last row( the latest date ) that corresponds to your group. This query should give you the result you want.
select A.ItemId,A.DATE,A.CurrentStock,A.ItemStockID from (select ItemId,MAX(DATE) DATE from items_stock where DATE <= '2010-11-05' AND Active = 1 group by ItemId) I, items_stock A where A.ItemStockID=( select MAX(ItemStockID) from items_stock where ItemID=I.ItemId and DATE=I.DATE and Active=1) ;
Upvotes: 1
Reputation: 1348
I want solution like this, but in optimized way
SELECT ISTI.ItemStockID, ISTI.DATE, ISTI.ItemID, ISTI.CurrentStock FROM items_stock AS ISTI
JOIN (SELECT MAX(ItemStockID) AS ItemStockID
FROM items_stock AS IST
JOIN (SELECT MAX(DATE) AS DATE, ItemID FROM items_stock WHERE DATE <= '2010-11-05' AND Active = 1 GROUP BY ItemID) AS SD ON SD.Date = IST.Date AND IST.ItemID = SD.ItemID
GROUP BY IST.ItemID
) AS ISTO ON ISTO.ItemStockID = ISTI.ItemStockID;
Upvotes: 0