Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql Server query not working as a subquery

Why is this query not working?

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;

It uses Northwind database. If I run it without creating a temporary view i.e if I run the query just contained within round brackets it runs just fine.

like image 438
TCM Avatar asked Jun 10 '26 23:06

TCM


2 Answers

at the first look i'd say because your Sum() doesn't have a column alias

like image 81
Mladen Prajdic Avatar answered Jun 17 '26 05:06

Mladen Prajdic


try this :

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
like image 22
Pranay Rana Avatar answered Jun 17 '26 05:06

Pranay Rana



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!