Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Stock age query to accept multiple rows as a resul

Author  Topic 

SergioM
Posting Yak Master

170 Posts

Posted - 2014-01-22 : 13:16:29
I worked with someone else to create a query that gives us the age of a stock. How long it has been in the warehouse since the Purchase order date (without completely selling out). It does exactly what I want, the problem is that it only accepts 1 row as a result.

The error message I get is:
quote:
Msg 512, Level 16, State 1, Line 4
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.


So my question is; can this code be modified to pass it multiple SKUS and run a report on every item currently in stock?


SELECT TOP 1 
@skuVar, CAST(GETDATE()-ReceivedOn AS INT) AS 'Age'
FROM
(SELECT pir.id AS id,aggregateQty AS aggregateQty,-qtyreceived as qtyreceived, (aggregateQty - qtyreceived) AS Diff, ReceivedOn AS ReceivedOn

,(
SELECT SUM (PurchaseItemReceive.qtyreceived)
FROM bvc_product pp
INNER JOIN PurchaseItemReceive on PurchaseItemReceive.ProductID = pp.id
WHERE pp.ID = p.id
AND PurchaseItemReceive.ReceivedOn < pir.ReceivedOn
GROUP BY PurchaseItemReceive.qtyreceived
) as 'preCalc'

,(
SELECT SUM (PurchaseItemReceive.qtyreceived)
FROM bvc_product pp
INNER JOIN PurchaseItemReceive on PurchaseItemReceive.ProductID = pp.id
WHERE pp.ID = p.id
AND PurchaseItemReceive.ReceivedOn < pir.ReceivedOn
GROUP BY PurchaseItemReceive.qtyreceived
)-AggregateQty as 'CalcNum'

FROM bvc_product p
INNER JOIN PurchaseItemReceive pir on pir.ProductID = p.id
WHERE p.ID = @skuVar
) A
WHERE CalcNum < 0


-Sergio
I use Microsoft SQL 2008

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2014-01-22 : 13:25:17
The short answer is yes. Please see the links below for how to post your sample data and expected output.

http://www.sqlservercentral.com/articles/Best+Practices/61537/
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page
   

- Advertisement -