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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:51:26+00:00 2026-05-17T22:51:26+00:00

DROP TABLE IF EXISTS `items_stock`; CREATE TABLE IF NOT EXISTS `items_stock` ( `ItemStockID` int(10)

  • 0
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

  • 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-17T22:51:27+00:00Added an answer on May 17, 2026 at 10:51 pm

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

DROP TABLE IF EXISTS student; CREATE TABLE student( bannerid VARCHAR(9) PRIMARY KEY NOT NULL
function populateDB(tx) { tx.executeSql('DROP TABLE IF EXISTS test'); tx.executeSql('CREATE TABLE IF NOT EXISTS test(course
I have the following table DROP TABLE IF EXISTS `test`.`foo`; CREATE TABLE `test`.`foo` (
I have a testdata like this: DROP TABLE SELECT_PASS; CREATE TABLE SELECT_PASS(ID INT(20),TESTCASE VARCHAR(20),RESULT
This is my Create table script: DROP TABLE IF EXISTS `wp_beats`; CREATE TABLE .`wp_beats`
i have this: DROP TABLE IF EXISTS `sf_guard_user`; CREATE TABLE `sf_guard_user` ( `id` INTEGER(11)
I currently have a table like this: Stuff ---------- StuffId identity int not null
Rather than create, and then drop a table in sqlite3, I would like to
This DROP TABLE IF EXISTS works, too bad that RENAME TABLE IF EXISTS doesn't
I have the following database -- MYSQL DROP TABLE IF EXISTS Attributes; DROP 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.