如何在表格udf中正确使用group by?

时间:2015-10-13 18:04:14

标签: sql-server user-defined-functions

我尝试创建一个表格接收开始日期和结束日期的表格UDF,然后显示该期间每个employee_ID所赚取的佣金。 我在这里尝试,但在employeeId附近得到的语法不正确。

create Function CommissionByDate(@start_d date,@end_d date)
Returns table
As
Return
(
Select Employee_ID
  ,sum(dbo.OfferCommission(Offer_value, Offer_date)) as Commission 
from Campaign_Offer
where Acceptance_status = 'yes' And Offer_date >= @start_d And Offer_date <=   
@end_d group by Employee_ID

1 个答案:

答案 0 :(得分:0)

您似乎忘记在GROUP BY Employee_ID

之后关闭括号
CREATE FUNCTION CommissionByDate
(
    @start_d DATE
    , @end_d DATE
)
RETURNS TABLE
AS
RETURN (
    SELECT Employee_ID, sum(dbo.OfferCommission(Offer_value, Offer_date)) AS Commission
    FROM Campaign_Offer
    WHERE Acceptance_status = 'yes'
        AND Offer_date >= @start_d
        AND Offer_date <= @end_d
    GROUP BY Employee_ID
    )

这应该有效