我正在嘗試爲rdlc報表創建一個Sp。我在其中使用許多用戶定義的函數進行計算。但是,當我試圖通過條款採取功能別名組是givng錯誤「消息207,級別16,狀態1,行14 無效的列名稱CommPaid「」下面是我的查詢Group By子句中列名錯誤
SELECT Employee.FirstName AS Tech, CommissionStructure.Name AS Commission, '$ ' + CONVERT(varchar(8), SUM(WorkOrderPayment.PaymentTotal))
AS TotalSales --, ISNULL(SUM(PurchaseOrder.AmountPaidToSupplier), 0) AS POAmount, ISNULL(SUM(WorkOrderPayment.TOC),0) AS TOC,
-- SUM(dbo.GetCashInvoiceAmount(WorkOrderPayment.WorkOrderPaymentID)) AS CashInvoice, ISNULL(SUM(dbo.GetSecTechAmount(WorkOrderPayment.WorkOrderID)),0) AS SecTechAmount
,dbo.GetCommission(SUM(WorkOrderPayment.PaymentTotal),SUM(dbo.GetCashInvoiceAmount(WorkOrderPayment.WorkOrderPaymentID)),ISNULL(SUM(PurchaseOrder.AmountPaidToSupplier), 0),ISNULL(SUM(WorkOrderPayment.TOC),0),ISNULL(SUM(dbo.GetSecTechAmount(WorkOrderPayment.WorkOrderID)),0),CommissionStructure.CommissionPercentage) As CommPaid
FROM WorkOrder INNER JOIN
WorkOrderPayment ON WorkOrder.WorkOrderID = WorkOrderPayment.WorkOrderID INNER JOIN
Employee ON WorkOrder.empID = Employee.empID INNER JOIN
CommissionStructure ON Employee.CommissionStructureID = CommissionStructure.CommissionStructureID INNER JOIN
[User] ON Employee.UserID = [User].UserID LEFT OUTER JOIN
PurchaseOrder ON WorkOrderPayment.WorkOrderPaymentID = PurchaseOrder.WorkOrderPaymentID
WHERE (WorkOrder.OrderStatusID = 3) AND (WorkOrder.Deleted = 0) AND ([User].Active = 1) AND (CONVERT(varchar, WorkOrder.ScheduleStartTime, 101)
>= '10/01/2013') AND (CONVERT(varchar, WorkOrder.ScheduleStartTime, 101) <= '10/24/2013')
GROUP BY Employee.FirstName, CommissionStructure.Name,CommPaid
當我刪除函數別名時,它會給出另一個顯而易見的錯誤。
"Msg 8120, Level 16, State 1, Line 4
Column 'CommissionStructure.CommissionPercentage' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."
查看[SELECT]的MSDN頁面的邏輯處理順序部分(http://technet.microsoft.com/en-us/) library/ms189499.aspx),並指出'GROUP BY'在'SELECT'前被邏輯*處理。這就是爲什麼你不能引用在你的GROUP BY中的'SELECT'子句中引入的別名。 –