将日期范围添加到现有查询

时间:2015-09-11 13:51:27

标签: date-range

我刚刚开始新工作,正在处理现有查询。由于我不是SQL专家,我想知道2015-8-1到2015-8-31之类的日期范围是否可以插入到下面的查询中。非常感谢您提供的任何帮助。

    SELECT 
 RANK() OVER (PARTITION BY DoctorFacility.ListName ORDER BY ApptSlot.Start)     
as SlotNumber
 , DoctorFacility.ListName as ProviderName
 , ApptSlot.Start as ApptStartTime
 , AppointmentsAlloc.Type as ApptType

 INTO #TEMP

 FROM CentricityPS.dbo.ApptSlot ApptSlot 
 INNER JOIN CentricityPS.dbo.AppointmentsAlloc AppointmentsAlloc 
ON ApptSlot.ApptSlotId=AppointmentsAlloc.ApptSlotId
 INNER JOIN CentricityPS.dbo.Schedule Schedule 
ON ApptSlot.ScheduleId=Schedule.ScheduleId
 INNER JOIN CentricityPS.dbo.DoctorFacility DoctorFacility 
ON Schedule.DoctorResourceId=DoctorFacility.DoctorFacilityId
 WHERE AppointmentsAlloc.Type IN
(
'Behavioral Health - 30' 
,'Behavioral Health 45' 
,'Established Patient - 15' 
,'Established Patient - 20' 
,'Fin Counsel - 30' 
,'Gyn Visit - 15' 
,'Pediatric Visit - 15'
) 
 AND ApptSlot.ListOrder=1 
 AND ApptSlot.Status IS  NULL  
 AND ApptSlot.Start>= GETDATE()
 ORDER BY DoctorFacility.ListName

SELECT 
ProviderName
, ApptStartTime
, ApptType

FROM #TEMP
WHERE SlotNumber = 3
ORDER BY ProviderName

DROP TABLE #TEMP

1 个答案:

答案 0 :(得分:0)

这里有两个单独的选择查询。无论如何,对于查询的任一部分,它都将以相同的方式完成。只需将其添加到WHERE语句

即可
  AND ApptSlot.Start>= 2015-8-1 
  AND ApptSlot.Start<= 2015-8-31

另一种语法是

  AND ApptSlot.Start between 2015-8-1 and 2015-8-31

理想情况下,您希望将这些开始日期和开始日期作为变量传递,因此值不会被硬编码。看起来可能更像这样。

   AND ApptSlot.Start between @StartDate and @EndDate