以下为引用的内容: SELECT Sales.Customer.CustomerID, Sales.SalesOrderHeader.SalesOrderID, SUM(Sales.SalesOrderDetail.LineTotal) AS SubTotalFROM Sales.Customer INNER JOIN Sales.SalesOrderHeader ON Sales.SalesOrderHeader.Customer ID = Sales.Customer.CustomerID INNER JOIN Sales.SalesOrderDetail ON Sales.SalesOrderDetail.SalesOrder ID = Sales.SalesOrderHeader.SalesOrderIDGROUP BY Sales.Customer.CustomerID, Sales.SalesOrderHeader.SalesOrder IDHAVING SUM(LineTotal) > 25000.00ORDER BY Sales.Customer.CustomerID, SalesOrderID ;
列表A
假设你盼看知道所有客户的均匀销售额。你可以应用列表B中的代码,它返回如下错误信息:
以下为引用的内容: SELECT Sales.SalesOrderHeader.SalesOrderID, AVG(SUM(Sales.SalesOrderDetail.LineTotal)) AS AverageFROM Sales.SalesOrderHeader INNER JOIN Sales.SalesOrderDetail ON Sales.SalesOrderDetail.SalesOrder ID = Sales.SalesOrderHeader.SalesOrder IDGROUP BY Sales.SalesOrderHeader.SalesOrderID
列表B
以下为引用的内容: Msg 130, Level 15, State 1, Line 1 Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
错误信息
以下为引用的内容: USE [AdventureWorks] GO /****** Object: UserDefinedFunction [dbo].[SalesTotals_fnt] Script Date: 12/09/2006 11:32:54 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE FUNCTION [dbo].[SalesTotals_fnt]() RETURNS TABLE AS RETURN (SELECT SalesOrderID, SUM(LineTotal)AS TotalSale FROM Sales.SalesOrderDetail GROUP BY Sales.SalesOrderDetail.SalesOrderID)
列表C
以下为引用的内容: CREATE FUNCTION [dbo].[AverageSale_fns] ( -- Add the parameters for the function here -- )RETURNS money ASBEGIN -- Declare the return variable here DECLARE @Result money -- Add the T-SQL statements to compute the return value here SET @Result =(SELECTAvg(TotalSale)AS AverageSale FROM dbo.SalesTotals_fnt()) -- Return the result of the function RETURN @Result END
列表D
以下为引用的内容: DECLARE @Avg moneySELECT @Avg = dbo.AverageSale_fns() SELECT *, @Avg as Average, TotalSale / @Avg as Ratio, CASE WHEN TotalSale / @Avg > 1 THEN 'Above Average'