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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:59:21+00:00 2026-06-09T16:59:21+00:00

Apologies on the heading, not sure I can explain this easily/well. I’ve inherited a

  • 0

Apologies on the heading, not sure I can explain this easily/well.
I’ve inherited a report that runs off a table (StockMovements). This table contains product number, movement type, and quantity (among others, those 3 are the important values).
The report is using a ProductMaster table and for each product number trying to group by product and then have a column for each movement type, summing all entries which match.
At the moment it is doing something like this:

SELECT PM.ProductNumber,
    (SELECT SUM(Quantity) FROM StockMovements WHERE MovementType='StockIn' AND ProductNumber=PM.ProductNumber) AS [StockIn],
    (SELECT SUM(Quantity) FROM StockMovements WHERE MovementType='StockOut' AND ProductNumber=PM.ProductNumber) AS [StockOut],
    (SELECT SUM(Quantity) FROM StockMovements WHERE MovementType='ClosingStock' AND ProductNumber=PM.ProductNumber) AS [ClosingStock]
FROM ProductMaster AS PM
GROUP BY PM.ProductNumber

This is killing the system as StockMovements is a massive table and as you can see it’s doing a new lookup for every product AND every movement type. I’m trying to modify the stored procedure to drive the SELECT from StockMovements grouped by ProductNumber, but am struggling to see how I can have each of the different columns summing values based on MovementType.
What I’d love is something like this:

SELECT ProductNumber,
    SUM(Quantity) WHERE MovementType='StockIn',
    SUM(Quantity) WHERE MovementType='StockOut',
    SUM(Quantity) WHERE MovementType='ClosingStock'
FROM StockMovement
GROUP BY ProductNumber

I’m more than willing to rewrite the whole this and drive it off whatever makes most sense, I’m just struggling to find a better way of doing it (please tell me it’s simple and I’ve just been starting at it way too long).

  • 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-09T16:59:23+00:00Added an answer on June 9, 2026 at 4:59 pm

    Close.

    SELECT ProductNumber,
        SUM(CASE WHEN MovementType='StockIn' THEN Quantity END),
        SUM(CASE WHEN MovementType='StockOut' THEN Quantity END),
        SUM(CASE WHEN MovementType='ClosingStock' THEN Quantity END)
    FROM StockMovement
    GROUP BY ProductNumber
    

    If there are other movement types, you might want to consider adding a WHERE clause to restrict to just the 3 types you’re interested in.

    If some products may have no rows for a movement type, you might want to wrap the SUM() in a COALESCE():

    COALESCE(SUM(CASE WHEN MovementType='StockIn' THEN Quantity END),0)
    

    to give a nicer total.

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

Sidebar

Related Questions

Apologies if this has been answered elsewhere but I'm pretty new and not sure
Apologies if the title is not clear, I' not really sure how to explain
Apologies, It looks like my original question was not able to correctly explain what
Apologies if this doesn't make sense, i'm not much of an experienced programmer. Consider
Apologies if this is a repost, I could not find the search terms to
Apologies for using someone else's brain for this, but I'm sure this is a
Apologies - am not good at SQL. Perhaps this is a simple query -
Apologies if this is a duplicate, but I've not managed to find this question
Apologies for this question but I can't find proper answer on the web. I'm
Apologies if this question has already been asked but I do not think I

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.