1
SELECT     
    ScheduleDays = COUNT(DISTINCT(CAST(datediff(d, 0, a.ApptStart) AS datetime)))
FROM 
    Appointments a
WHERE   
    ApptKind = 1 AND 
    --filter on current month
    a.ApptStart >= ISNULL(DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0),'1/1/1900')   AND
    a.ApptStart < ISNULL(DATEADD(month, DATEDIFF(month, 0, GETDATE())+1, 0),'1/1/3000')AND
    --filter all days that aren't Friday, and then give you all Fridays that have an hour > 12.
    DATENAME(weekday, a.ApptStart) <> 'Friday' and DATEPART(hour, a.ApptStart) > 12 AND
    --Filter on doctor
    a.ResourceID in (201)

此查询将查看预约开始时间,而不是将星期五计算在内,因为我们的 Docs 仅在星期五工作半天。有人告诉我,我们确实想计算它们,但只有半天(第一次我被告知要排除它们,哈哈)。

有人可以帮我写一份案例陈述,将中午 12 点之后没有预约的星期五算作半天吗?我相信它必须进入ScheduleDays=COUNT(DISTINCT(CAST(datediff(d,0,a.ApptStart) as datetime))). 如果我们无论如何都要用例的话,也许我们可以把 Friday 和 after 12 个过滤器放在那里而不是放在 where 子句中。ScheduleDays=COUNT(DISTINCT CASE WHEN etc. 我真的很感激帮助。

4

1 回答 1

3

您不能真正使用 计算一半的东西count,所以这不是要走的路。但是,你可以用算术来做到这一点。我认为是这样的:

select (count(distinct (case when DATENAME(weekday, a.ApptStart) <> 'Friday'
                             then cast(a.apptstart as date)
                        end)
             ) +
        0.5 * count(distinct (case when DATENAME(weekday, a.ApptStart) = 'Friday'
                                   then cast(a.apptstart as date)
                              end)
                   )
       ) as ScheduleDays

如果文档只在星期五工作半天,我认为您不需要检查预约时间。当然,如果您愿意,可以将其添加到第二个count.

请注意,为了计算天数,我使用了更简单的语法,将 转换datetimedate.

编辑:

通过小时检查:

select (count(distinct (case when DATENAME(weekday, a.ApptStart) <> 'Friday'
                             then cast(a.apptstart as date)
                        end)
             ) +
        0.5 * count(distinct (case when DATENAME(weekday, a.ApptStart) = 'Friday' and  DATEPART(hour, a.ApptStart) <= 12 
                                   then cast(a.apptstart as date)
                              end)
                   )
       ) as ScheduleDays
于 2013-07-17T14:32:12.170 回答