I have a problem with an SQL request I am working on.
I would like to return the total number of a certain item that is ordered by a certain customer. The problem is: I get more than one line when I execute my request.
(Please note that this is a very simplified version of my request to help the comprehension of the code. My tables are not named like this, neither are my rows.)
SELECT outerpallet.pallet as palletnbr, ID, shipmentID FROM arandomtable
OUTER APPLY (SELECT SUM(palletnbr) as pallet FROM thepallettable GROUP BY arandomtable.ID) as outerpallet
WHERE ID = @something
What I get:
palletnbr ID shipmentid
1 2 340
2 3 340
3 4 340
What I would like to get (whitout necessarily having the ID and shipmentid):
palletnbr
6
To do this I would have to get all my lines in one. I thought of doing another outer apply, but it won't work (since I am doing an outer apply of another outer apply)...
I thought of maybe doing my outer apply in another way, but I can't seem to find out how. I tried to add:
GROUP BY shipmentid
But it would have to be in the OUTER APPLY, and I can't seem to figure out how to put it there.
Would anyone have an idea that could help me to solve this problem ? Any little hint would be of a great help !