4

我正在尝试为 rdlc 报告创建 Sp。在其中我使用许多用户定义的函数进行计算。但是,当我尝试在 group by 子句中使用函数别名时,它会给出错误“Msg 207,Level 16,State 1,Line 14 Invalid column name 'CommPaid'” 下面是我的查询

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."
4

2 回答 2

5

将您在 SELECT 中使用的复杂函数也放入 GROUP BY:

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

关键是,您的别名在 GROUP BY 处理期间不可用。SELECT 通常是要执行的语句的最新部分......

于 2013-10-29T06:29:22.990 回答
1

删除CommPaid_GROUP BY

GROUP BY Employee.FirstName, CommissionStructure.Name

因为它本身也是一个Aggregate领域

但是,如果您想要两个聚合字段,即TotalSales, CommPaid- 您有两个选择:

选项 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
于 2013-10-29T06:28:08.440 回答