当FollowUpVisit列等于N时,平均DateDiff

时间:2016-10-28 15:07:42

标签: sql sql-server

这里的代码我试图无济于事。

Select   
g.StopCodeName,
AVG(DateDiff(Day, DesiredAppointmentDate,AppointmentDateTime))Case When FollowUpVisitFlag = 'N' THEN  ELSE NULL END AS [AvgNewWt]

请注意,我正在尝试通过StopCode对它们进行分组。请帮助!!

1 个答案:

答案 0 :(得分:0)

这不是任何DBMS中的有效语法。将CASE语句移到AVG聚合

试试这种方式

SELECT g.StopCodeName,
       Avg(CASE
             WHEN FollowUpVisitFlag = 'N' THEN Datediff(Day, DesiredAppointmentDate, AppointmentDateTime)
             ELSE NULL
           END) AS [AvgNewWt]