r/learnSQL • • Oct 16 '25

Question: [SQL Server] Creating a view to identify Inbound, Outbound, and Opening Stock movements based on the first date of each month

4 Upvotes

Hey everyone,
I’m working in SQL Server and I have a table with the following structure:

SELECT 
    [Item_Code],
    [Company_Code],
    [Plant_Code],
    [Location_Code],
    [Quantity],
    [Date],
    [Standard_Cost],
    [Average_Cost]
FROM [stga1].[T1_JDE_Fact_Plant];

This table contains inventory movements for different items and plants, along with their quantities, costs, and transaction dates.

I’d like to create a view that shows all these columns plus a new column called Movements, which classifies each record as one of the following:

  • Opening Stock → the first movement recorded in that month for each unique combination of item/plant/location,
  • Inbound → when Quantity > 0,
  • Outbound → when Quantity < 0.