mardi 17 mars 2015

Using several subqueries in sql statement

I am trying to use several subqueries but I am getting an error. It works if I remove the second subquery.


Error: Msg 512, Level 16, State 1, Line 2 Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.



USE SUPERSTARIS
SELECT lines.Item,
Inventory.ItemAlternative,
Inventory.OnHandPhys,
Inventory.Allocated,
Inventory.CostPrice,
replace(vInventoryOptional2Values.Value, '/', '') AS Brand,
Inventory.Optional3 AS StockStatus,

(SELECT SUM(l.Invoiced) * -1
FROM lines l
WHERE l.Date >= '2014-01-01'
AND l.Date <= '2014-01-31'
AND l.Item = lines.Item) AS QtySoldInPeriod,

(SELECT SUM(lines.AmountBase-lines.CostAmount)*-1
FROM lines l
WHERE l.Date >= '2014-01-01'
AND l.Date <= '2014-01-31'
AND l.Item = lines.Item) AS QtySoldInPeriod2
FROM lines
INNER JOIN inventory ON lines.item = Inventory.item
INNER JOIN vInventoryOptional2Values ON Inventory.Optional2 = vInventoryOptional2Values.RecordID
WHERE lines.Invoiced < 0
AND Inventory.Status = 0
AND Inventory.Optional3 <> 3
GROUP BY Lines.Item,
Inventory.Optional3,
Inventory.ItemAlternative,
Inventory.OnHandPhys,
Inventory.CostPrice,
vInventoryOptional2Values.Value,
Inventory.Allocated

Aucun commentaire:

Enregistrer un commentaire