2017-07-02 55 views
-1
Select c.CustomerName, pl.ProductName as 'Product Name', o.OrderDate as 'Order date', 
Count([Address]) as 'No of Places', SUM(pl.Price * od.OrderQty) as 'Total Amount' 
From Customer c 
Inner Join [Order] o ON o.CustomerId = c.CustomerID 
Inner Join OrderDetails od ON od.OrderId = o.OrderID 
Inner Join ProductList pl ON pl.ProductID = od.ProductId 
where c.CustomerID = (select cc.CustomerID from Customer cc where 
cc.CustomerName = 'Mr.A') 

這個代碼,但顯示該錯誤當我運行以下SQL代碼時顯示此錯誤?我試圖解決由子查詢

消息8120,級別16,狀態1,行 列「Customer.CustomerName」在選擇列表中,因爲無效它不包含在聚合函數或GROUP BY子句中。

+3

(求和,計數...等),你必須使用組在其餘列你選擇 –

回答

1

你已經忘了指定group by子句,試試這個:

Select c.CustomerName, pl.ProductName as 'Product Name', o.OrderDate as 'Order date', 
Count([Address]) as 'No of Places', SUM(pl.Price * od.OrderQty) as 'Total Amount' 
From Customer c 
Inner Join [Order] o ON o.CustomerId = c.CustomerID 
Inner Join OrderDetails od ON od.OrderId = o.OrderID 
Inner Join ProductList pl ON pl.ProductID = od.ProductId 
Inner join Customer cc on c.CustomerID=cc.CustomerID and cc.CustomerName = 'Mr.A' 
group by c.CustomerName, pl.ProductName, o.OrderDate 
每次使用聚合函數,如時間
+0

但我想解決這個問題使用SubQuery,你沒有在這段代碼中使用。 –

+0

其相同,子查詢或不...使用您的查詢,但添加與我的查詢 – Esperento57

+0

列最後我解決它,謝謝。 –

相關問題