在sql语句中使用几个子查询

时间:2015-03-17 08:31:37

标签: sql sql-server-2005 subquery

我正在尝试使用多个子查询,但是我收到了一个错误。如果我删除第二个子查询,它就可以工作。

错误:

  

Msg 512,Level 16,State 1,Line 2子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。

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

1 个答案:

答案 0 :(得分:0)

正如Coder of Code所写,错误是您在子查询中引用主查询表而不是子查询表。因此,您可以快速纠正错误。 但是您已经在主查询中按lines.item进行分组,因此您不需要额外的子查询来获取SUM。

您可以简化查询 - 删除子查询:

USE SUPERSTARIS
SELECT lines.Item,
    Inventory.ItemAlternative,
    Inventory.OnHandPhys,
    Inventory.Allocated,
    Inventory.CostPrice,
    replace(vInventoryOptional2Values.Value, '/', '') AS Brand,
    Inventory.Optional3 AS StockStatus,
    -SUM(lines.Invoiced) AS QtySoldInPeriod,
    -SUM(lines.AmountBase-lines.CostAmount) 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 lines.Date >= '2014-01-01'
    AND lines.Date <= '2014-01-31'
    AND Inventory.Status = 0
    AND Inventory.Optional3 <> 3
GROUP BY Lines.Item,
    Inventory.Optional3,
    Inventory.ItemAlternative,
    Inventory.OnHandPhys,
    Inventory.CostPrice,
    vInventoryOptional2Values.Value,
    Inventory.Allocated

并做进一步的优化:

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