仅显示特定时间段的记录

时间:2018-06-25 14:43:05

标签: sql sql-server

我将数据集分为8:00 am、4:30pm和8:30 pm的3个时间间隔/班次,如下所示:

CASE 
WHEN cast(last_update_date as time) >= '20:31' THEN '8:00' 
   WHEN cast(last_update_date as time) < '8:01' THEN 'As of 08:00' 
   WHEN cast(last_update_date as time) >= '8:01' AND cast(last_update_date as 
time) < '16:31' THEN 'As of 16:30'
 WHEN cast(last_update_date as time) >='16:31' and cast(last_update_date as 
time) < '20:31' THEN 'As of 20:30'
 END TimeInterval

我遇到的问题是我不想在尚未完成的时间间隔内显示记录。

例如,如果我在8:02 AM运行查询,则将在8:01和8:02 AM更新的记录放在“截至4:30 pm”组中。但是4:30 pm还没有“通过”,因此这些记录无法显示在我的数据集中。

这也需要对日期敏感。例如,如果我在星期二的8:02 AM运行查询,则需要查看星期一发生的所有记录,然后在星期二的8:00 AM看到记录,但此后没有。我已经将我的数据限制为仅显示当前一周,因此我无需考虑以前的几周,而只需考虑从星期一开始的那一周。

我已经可以通过检查getdate()的星期几并使用以下内容与我的更新日期进行比较来完成第二部分工作:

datepart(weekday, getdate()) today_day, datepart(weekday, last_update_date) update_day

然后在我的where子句中使用

today_day <= update_day 

这将显示今天的数据以及一周中的前几天

我尝试用今天的时间做同样的事情:

cast(getdate()  as time) today_time

但是我无法编写案例声明以进行更改以与更新日期时间进行比较。即。我无法在case语句中执行此操作,也无法将时间戳更改为特定的时间戳,例如:

WHEN cast(last_update_date as time) >= '20:31' THEN cast('8:00' as time) 

Below is the final output (that is put into Crytal report). In this case, the report was run on Thursday sometime in the morning, we do not want to show those circled numbers until 4:30pm has actually happened on Thursday, even if the user runs the report.

下面是我的工作查询(还有很多其他东西)

 IF OBJECT_ID('tempdb..#Final') IS NOT NULL DROP Table #Final
SELECT getdate() today_date, cast(getdate()  as time) today_time,  
datepart(weekday, getdate()) today_day, 
DateName(weekday, last_update_date) Day_of_week_name, datepart(weekday, 
last_update_date) update_day, cast(last_update_date as time) last_update_date_Time,
CASE 
    WHEN cast(last_update_date as time) >= '20:31' THEN '8:00' 
       WHEN cast(last_update_date as time) < '8:01' THEN 'As of 08:00' 
   WHEN cast(last_update_date as time) >= '8:01' AND cast(last_update_date as time) 
< '16:31' THEN 'As of 16:30'
   WHEN cast(last_update_date as time) >='16:31' and cast(last_update_date as time) 
< '20:31' THEN 'As of 20:30'
   END TimeInterval
   ,
   CASE 
    WHEN cast(last_update_date as time) >= '20:31' THEN '08:00' 
   WHEN cast(last_update_date as time) < '8:01' THEN '08:00'
   WHEN cast(last_update_date as time) >= '8:01' AND cast(last_update_date as time) 
< '16:31' THEN '16:30'
   WHEN cast(last_update_date as time) >='16:31' and cast(last_update_date as time) 
< '20:31' THEN '20:30'
   END TimeInterval2
   ,

     CASE WHEN
 requisition_status_Description  = 'Pending' THEN 'Active'
 WHEN requisition_status_Description  = 'Created' THEN 'Active'
 WHEN requisition_status_Description  = 'Requested' THEN 'Active'
 WHEN requisition_status_Description  = 'Approved' THEN 'Completed'
 WHEN requisition_status_Description  = 'Denied' THEN 'Completed'
 WHEN requisition_status_Description  = 'Cancelled' THEN 'Completed'
 END RequisitionStatus
 ,
  CASE WHEN
 cast(last_update_date as time) < '16:30' then 'Regular'
 WHEN  cast(last_update_date as time) >= '16:30'  then 'Extended Hours'
 End ShiftTime, @StartWeek StartWeek, @EndWeek EndWeek
 ,*
 FROM #DataSet

0 个答案:

没有答案