2013-10-29 93 views
2

我正在嘗試爲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." 
+0

查看[SELECT]的MSDN頁面的邏輯處理順序部分(http://technet.microsoft.com/en-us/) library/ms189499.aspx),並指出'GROUP BY'在'SELECT'前被邏輯*處理。這就是爲什麼你不能引用在你的GROUP BY中的'SELECT'子句中引入的別名。 –

回答

3

將您正在使用的SELECT還融入了複雜的功能GROUP BY:

GROUP BY 
... 
,dbo.GetCommission(... 

問題是,您的別名在GROUP BY過程中不可用噝聲。 SELECT通常是要執行的語句的最新部分...

+0

Radim您可以在上面的答案評論中看到錯誤。 – Haris

+0

好吧,我只是設法瞭解你的GetCommision函數的內容。正如我目前看到的那樣,您應該將執行分爲兩個查詢。第一個GROUP BY * Employee.FirstName,CommissionStructure.Name *返回三個值。然後在結果上面創建一個GROUP BY,將** CommPaid **的內容作爲已處理的* column *提供。我們正在收回的提示非常清楚。我們試圖將SUMed值傳遞給函數...依賴於GROUP BY。這只是一次運行就不能成功。我們首先必須分組才能正確得到SUM –

+0

@Haris - 只是一個方面的評論 - 你應該避免在堆棧溢出時引用位置的答案。我相信投票(和相同的投票計數隨機化,我相信)可以並且確實會改變答案的顯示順序 - 所以當人們閱讀您的評論時,目前「上面的答案」可能不會。我通常會建議一個鏈接(這是每個答案下面的共享選項給你的),或者至少指的是你想要引用的答案的用戶。 –

0

GROUP BY

GROUP BY Employee.FirstName, CommissionStructure.Name 

刪除​​,因爲它本身也是一個Aggregate

但是,如果你希望兩個總場,也就是TotalSales,​​- 你有兩個選項:

選項1:包括在複雜的功能時,您GROUP BY

GROUP BY Employee.FirstName, CommissionStructure.Name, 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) 

選項2:讓兩個查詢和JOIN兩個,如:

SELECT A.Tech, A.Commission, A.TotalSales, B.CommPaid FROM 
(
SELECT Employee.EmpID, Employee.FirstName AS Tech, CommissionStructure.Name AS Commission, '$ ' + CONVERT(varchar(8), SUM(WorkOrderPayment.PaymentTotal)) 
       AS TotalSales 

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 
) A 

INNER JOIN 
SELECT A.Tech, A.Commission, A.TotalSales, B.CommPaid 
(
SELECT Employee.EmpID, Employee.FirstName AS Tech, 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 
) B 
ON A.EmpID = B.EmpID 
+0

使用選項1「Msg 144,Level 15,State 1,Line 14時出錯」 不能在用於GROUP BY子句的group by列表的表達式中使用聚合或子查詢。 – Haris

+0

當使用第二個選項時,它也給出了與第二個查詢相關的一些語法錯誤。 「Msg 156,Level 15,State 1,Line 21 關鍵字'SELECT'附近的語法不正確 Msg 156,Level 15,State 1,Line 23 關鍵字'SELECT'附近的語法不正確 Msg 102,Level 15 ,狀態1,行37 ')'附近語法錯誤。 「 – Haris