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).
Close.
If there are other movement types, you might want to consider adding a
WHEREclause 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 aCOALESCE():to give a nicer total.