在 AdventureWorks2012 数据库中,我必须编写一个查询来显示 Sales.SalesOrderHeader 表中的所有列以及 Sales.SalesOrderDetail 表中的平均 LineTotal
尝试 1
SELECT *
FROM Sales.SalesOrderHeader
(SELECT AVG (LineTotal)
FROM Sales.SalesOrderDetail
WHERE LineTotal <> 0)
GROUP BY 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.
子查询让我很困惑。我究竟做错了什么?谢谢。