Sql Server查询不能作为子查询工作

时间:2010-06-30 12:20:12

标签: sql-server-2008

为什么此查询不起作用?

Select temp.CompanyName from

(
    SELECT  c.CompanyName, o.OrderID, 
            YEAR(o.OrderDate)  As YEAR, 
            Sum(od.UnitPrice * od.Quantity) from Orders o 
        INNER JOIN [Order Details] od 
            ON o.OrderID = od.OrderID
        INNER JOIN Customers c
            On c.CustomerID = o.CustomerID
                GROUP BY o.OrderId,c.CompanyName, YEAR(o.OrderDate)

) As temp;

它使用Northwind数据库。如果我在没有创建临时视图的情况下运行它,即如果我运行仅包含在圆括号内的查询,则运行正常。

2 个答案:

答案 0 :(得分:5)

第一次看我会说因为你的Sum()没有列别名

答案 1 :(得分:1)

试试这个:

Select CompanyName from
(
    SELECT  c.CompanyName, o.OrderID, 
            YEAR(o.OrderDate)  As YEAR, 
            Sum(od.UnitPrice * od.Quantity) as price from Orders o 
        INNER JOIN [Order Details] od 
            ON o.OrderID = od.OrderID
        INNER JOIN Customers c
            On c.CustomerID = o.CustomerID
                GROUP BY o.OrderId,c.CompanyName, YEAR(o.OrderDate)

)  temp