Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Still Having Trouble With This Query
Message
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01402654
Message ID:
01402661
Views:
37
How do you know which record is earlier than other?
By InventoryId?
And what if you have several records for that PartId?
Maybe it is time to add one more field in your table that show the status of the record so the table would look like this:
InventoryId PartId  WarehouseId LocationId  LocationInfo  SerialNo  Quantity Received
----------- ------- ----------- ----------- ------------- --------- -------- --------
2254        1       2           6           Some Info     ABC-123   1          1
2138        1       2           0           NULL          XYZ-456   1          1 
2137        1       1           1           More Info     NULL      1          1
And when the part is NOT received:
InventoryId PartId  WarehouseId LocationId  LocationInfo  SerialNo  Quantity Received
----------- ------- ----------- ----------- ------------- --------- -------- --------
2138        1       2           0           NULL          XYZ-456   1          0
2137        1       1           1           More Info     NULL      1          1
Then when the Warehouse receives that Part you should update that field to 1 for the empty record and then add a new record.

After you did this the SP will be simple:
CREATE PROCEDURE ap_GetPartsNotReceived
	@WarehouseId INT

AS
BEGIN

	SELECT  p.PartNo, i.SerialNo AS Serial, l.Location, i.LocationInfo, i.PurchaseOrder, 
                      p.Description, u.FullName, 
		      w.Caption AS Warehouse, w.WarehouseCode, i.*
		FROM Inventory i
		JOIN Parts p ON p.PartId = i.PartId
		JOIN Warehouses w ON w.WarehouseId = i.WarehouseId
		JOIN Users u ON u.UserId = i.UserId
		FULL JOIN Locations l ON l.LocationId = i.LocationId
		WHERE i.WarehouseId = @WarehouseId AND
                      i.LocationId  = 0            AND
                      i.Received    = 0
		ORDER BY p.PartNo, i.SerialNo
>I'm have this Inventory table
>
>
>InventoryId PartId  WarehouseId LocationId  LocationInfo  SerialNo  Quantity
>----------- ------- ----------- ----------- ------------- --------- ---------
>2254        1       2           6           Some Info     ABC-123   1
>2138        1       2           0           NULL          XYZ-456   1
>2137        1       1           1           More Info     NULL      1
>
>
>
>I wrote this query to pull parts that have not been received. Not received is defined as LocationId = 0. You can see in the
>above data set that part 1 was sent from warehouse 1 to warehouse 2. At warehouse 2 it was put on location 6.
>
>When I call this query again, I get back the second record. I should not get any records back from this data set because the
>newest record has it on a location, so therefore it's received.
>
>Here's my query:
>
>
>CREATE PROCEDURE ap_GetPartsNotReceived
>	@WarehouseId INT
>
>AS
>BEGIN
>
>	SELECT  p.PartNo, i.SerialNo AS Serial, l.Location, i.LocationInfo, i.PurchaseOrder, 
>                      p.Description, u.FullName, 
>		      w.Caption AS Warehouse, w.WarehouseCode, i.*
>		FROM Inventory i
>		JOIN Parts p ON p.PartId = i.PartId
>		JOIN Warehouses w ON w.WarehouseId = i.WarehouseId
>		JOIN Users u ON u.UserId = i.UserId
>		FULL JOIN Locations l ON l.LocationId = i.LocationId
>		WHERE i.WarehouseId = @WarehouseId AND
>			  i.LocationId = 0
>		ORDER BY p.PartNo, i.SerialNo
>
>
>
>What do I need to do to adjust it so it meets the requirements?
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Previous
Reply
Map
View

Click here to load this message in the networking platform