gpt4 book ai didi

sql - Group By 子句中的列名无效错误

转载 作者:行者123 更新时间:2023-12-03 21:37:24 24 4
gpt4 key购买 nike

我正在尝试为 rdlc 报告创建一个 Sp。其中我使用了许多用户定义的函数进行计算。但是当我试图在 group by 子句中使用函数别名时,它给出了一个错误“Msg 207, Level 16, State 1, Line 14
无效的列名 '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."

最佳答案

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

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

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

关于sql - Group By 子句中的列名无效错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19651110/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com