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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:25:41+00:00 2026-05-27T03:25:41+00:00

I have a table containing inventory ID | Product ID | In_Transit | Quantity

  • 0

I have a table containing inventory

ID   |   Product ID   |  In_Transit  |   Quantity   | Cumulative Quantity
=====+================+==============+==============+====================
1    |      1         |     0        |     1000     |      1000
2    |      1         |     0        |        1     |      1001
3    |      1         |     1        |       54     |      1055
4    |      1         |     1        |        1     |      1056

So the total inventory for product id 1 is ‘1056’ I get this using a SELECT MAX(ID) subquery join with the table to get its cumulative quantity which is 1056.

I would like to get the Inventory total (subtracting all the amounts in transit)

So 1056 – 54 – 1 = 1001

How would I get this in one query so i get

Product ID | Total Inventory | Inventory on Hand (Excluding in Transit |
===========+=================+=========================================    
1          |     1056        |           1001

Also i need to use the cumulative inventory to get the total as opposed to ‘SUM’, except for summing those in transit because (those not in transit) have a large number of records and they take ages to SUM. I can use it to sum those in transit because there are far fewer records

  • 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-27T03:25:42+00:00Added an answer on May 27, 2026 at 3:25 am
    SELECT 
        a.product_id
      , a.cumulative as total_inventory
      , a.cumulative - COALESCE(b.quantity,0) AS inventory_on_hand
    FROM table1 a
    JOIN 
        ( SELECT MAX(id) AS max_id
          FROM table1
          GROUP BY product_id
        ) m ON (m.max_id = a.id)
    LEFT JOIN
        ( SELECT product_id, SUM(quantity) 
          FROM table1 
          WHERE in_transit = 1
          GROUP BY product_id
        ) b ON (a.product_id = b.product_id)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table containing numbers, range between 1-1000 they are unique, but I
I have a table containing records of the date and time each product was
I have a table containing millions of transaction records(Obj1) which looks like this TransactionNum
I have a table containing prices for a lot of different things in a
I have a table containing the runtimes for generators on different sites, and I
I have a table containing hundreds of entries and I am trying to delete
I have a table containing cells with text of various lengths. It is essential
I have a table containing 10 records. I want to fetch 5th and the
I have a table containing data about some users. Many of them use our
I have a table containing reports and the date/time they were created. I'd like

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.