I have 2 table variables @Items and @Locations. Both tables contain a volume column for items and locations.
I am trying to retrieve locations with the available volume capacity per item. I would like the location to be excluded from the results if it has no more volume capacity to hold any other item. The goal is to update @Items with available locations based on the volume and ignore the location if it can't store any more items.
Here is the T-SQL that I have written for this scenario:
DECLARE @Items TABLE
(
Id INT,
Reference NVARCHAR(255),
Volume DECIMAL(18, 4),
IdLocation INT
)
DECLARE @Locations TABLE
(
Id INT,
Reference NVARCHAR(255),
Volume DECIMAL(18, 4)
)
INSERT INTO @Locations (Id, Reference, Volume)
SELECT 100, 'Location 1', 50000
UNION SELECT 101, 'Location 2', 100000
UNION SELECT 102, 'Location 3', 300000
INSERT INTO @Items (Id, Reference, Volume)
SELECT 1, 'Item 1', 50000
UNION SELECT 2, 'Item 2', 50000
UNION SELECT 3, 'Item 3', 100000
UNION SELECT 4, 'Item 4', 100000
UNION SELECT 5, 'Item 5', 300000
UNION SELECT 6, 'Item 6', 300000
UNION SELECT 7, 'Item 7', 300000
UPDATE I
SET I.IdLocation = (SELECT TOP 1 L.Id
FROM @Locations L
WHERE L.Volume >= I.Volume)
FROM @Items I
SELECT *
FROM @Items
The results which I get:

The results which I expect to get:

If anyone has a solution to this problem I would be very grateful.