Saturday, 15 September 2012

sql server 2005 - Using several subqueries in sql statement -


I'm trying to use several subqueries but I'm getting an error This works if I use the second subkey I remove

Error:

Message 512, Level 16, State 1, Line 2 Subkey returned more than 1 value. !! =,>,> =>,

  is used as the SUPERSTARIS subquery expression, when the subquery is =, =, & lt;, & lt; not allowed as lines.Item, Inventory.ItemAlternative, Inventory.OnHandPhys, Inventory.Allocated, select Inventory.CostPrice to replace (vInventoryOptional2Values.Value, '/', '') Inventory.Optional3 brand StockStatus In, (select yoga (l.Invoiced) * -1 lines l ou = l.Date> = '2014-01-01' and l.Date & lt; = '2014-01-31' and l Item = lines.Item) in the form of QtySoldInPeriod, (select the yoga (lines.mountbaz-line.kas Amount) * - In lines 1 from where I am L.Det> = '2014-01-01' and L.Det.LT; = '2014-01-31' and L.ITIM = Lines. Items) from lines Quantity Solid Inboard Include INNER inventory on 2 lines. ITEM = Inventory.it INNER Inventory. Add vInventoryOptional2Values ​​on Tional2 = vInventoryOptional2Values.RecordID where lines. Invoiced & lt; 0 more Inventory.Status = 0 and Inventory.option3 & lt; & Gt; Lines.Item, Inventory.Optional3, Inventory.ItemAlternative, Inventory.OnHandPhys, Inventory.CostPrice, vInventoryOptional2Values.Value, 3 group by Inventory.Allocated  

is written in the form of code code, error is the main query table reference in the subquery instead of the subquery table, so you can improve your error immediately but you already have the main query You create groups based on lines, so you need an additional subkey to get the SUM Is not there.

If your query can simplify - subquery Remove:

  Use SUPERSTARIS selection lines.Item, Inventory.ItemAlternative, Inventory.OnHandPhys, Inventory.Allocated, the Inventory.CostPrice, place (vInventoryOptional2Values.Value, '/', '') as the brand, StockStatus, -SUM (lines.Invoiced) QtySoldInPeriod, -SUM (lines.AmountBase-lines.CostAmount) as QtySoldInPeriod2 In the form of inventory.Optional3 lines from the lines to join in the list of lines.itm = inventory.item inventory.Official 2 = viantivarivostational 2 value. Lines of Recordside include XIntooptional 2 Values. Exchange and lieutenant; 0 more lines.Date & gt; = '2014-01-01' and lines.Det and Lits; = '2014-01-31' and Inventory.Status = 0 and Inventory Optional 3 & lt; & Gt; Lines.Item, Inventory.Optional3, Inventory.ItemAlternative, Inventory.OnHandPhys, Inventory.CostPrice, vInventoryOptional2Values.Value, 3 Group  

Work on optimizing Inventory.Allocated:

  brands use as SUPERSTARIS selection Inventory.Item, Inventory.ItemAlternative, Inventory.OnHandPhys, Inventory.Allocated, Inventory.CostPrice, place (vInventoryOptional2Values.Value, '/', ''), Inventory .Optional3 as StockStatus, from l.QtySoldInPeriod, from l.QtySoldInPeriod2 (select lines.item, -SUM (lines.Invoiced) QtySoldInPeriod, SUM (lines.CostAmount - lines.AmountBase) as lines QtySoldInPeriod2 ou lines.Invoiced & LT; 0 and lines.Date & gt; = '2014-01-01' and lines.Date & lt; = '2014-01-31' c Featured by Lines.Item) Join l.item = Inventory.item and Inventory.Status = 0 and List L interiors. Optional 3 & lt; & Gt; 3 Inventory.Optional2 = vInventoryOptional2Values.RecordID  
vInventoryOptional2Values ​​on Interiors

No comments:

Post a Comment