0
在AdventureWorks2012數據庫,我必須寫一個查詢,顯示從Sales.SalesOrderHeader表並從Sales.SalesOrderDetail表子查詢Group By子句
嘗試1
SELECT *
FROM Sales.SalesOrderHeader
(SELECT AVG (LineTotal)
FROM Sales.SalesOrderDetail
WHERE LineTotal <> 0)
GROUP BY LineTotal
平均LineTotal所有列
我得到以下錯誤:
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'SELECT'.
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ')'.
嘗試2
SELECT *
FROM Sales.SalesOrderHeader h
JOIN (
SELECT AVG(LineTotal)
FROM Sales.SalesOrderDetail d
GROUP BY LineTotal) AS AvgLineTotal
ON d.SalesOrderID = h.SalesOrderID
我得到以下錯誤:
Msg 8155, Level 16, State 2, Line 7
No column name was specified for column 1 of 'AvgLineTotal'.
Msg 4104, Level 16, State 1, Line 7
The multi-part identifier "d.SalesOrderID" could not be bound.
子查詢是對我來說非常混亂。我究竟做錯了什麼?謝謝。
來吧,你要問你的任務的每個人? http://stackoverflow.com/questions/21096582/sql-subqueries-errors,http://stackoverflow.com/questions/20501110/sql-total-quanity-purchased-by-year,http://stackoverflow.com/問題/ 20499184/sql-total-quantity-and-sum – Lamak