sql-serverwhere-clausecasedate-arithmetic

SQL Server displaying certain rows with a case inside of a where


I have a SQL Server query that needs to display information based on the time of day. I am trying to select rows based on shift (Day and Night shift).

I have a general table built aliased as DATA, and then I select from DATA because of issues with aliasing columns... From there I have a WHERE Clause with a CASE inside of it. That Case needs to look at the time of day and get the current hour, if that hour is in the day shift then I want to display only the day shift rows. If that hour is in the night shift then I want to display only the night shift rows.

Day shift is represented by 1

Night shift is represented by 0

Here is the query, the main issues I am having is with the second SELECT...

WITH DATA as(
    select vwDateShift.*
        ,DateHour.DateTime
        ,DateHour.DateTimeEnd
        ,DateHour.Hour
        ,DateHour.HH
        ,DateHour.[HH:MM]
        ,DATEDIFF(Hour,DateTime,GETDATE()) as HoursAgo
        ,CASE
            WHEN hour <= 6 THEN 0
            WHEN hour >= 7 And hour <=18 THEN 1
            ELSE 0
         END AS WhichShift
        ,Case
            WHEN hour <= 6 Then ShiftNightPrev
            WHEN hour >= 19 Then ShiftNight
            ELSE ShiftDay
        end AS ShiftHour
    from PublicUtilities..vwDateShift
    Left Join PublicUtilities..DateHour on DateHour.Date = vwDateShift.Date
    )
    SELECT * 
    FROM DATA
    WHERE WhichShift = CASE
        WHEN DATEPART(HOUR, GETDATE()) = 1 THEN WhichShift
        WHEN DATEPART(HOUR, GETDATE()) = 2 THEN WhichShift
        WHEN DATEPART(HOUR, GETDATE()) = 3 THEN WhichShift
        ...
        END

There are 24 WHEN cases inside of the CASE but to save room I left them out because they are very similar.

 Date Time                  HH  Which Shift
-----------------------     --  -----------
2000-01-06 04:00:00.000     04  0
2000-01-06 05:00:00.000     05  0
2000-01-06 06:00:00.000     06  0
2000-01-06 07:00:00.000     07  1
2000-01-06 08:00:00.000     08  1
2000-01-06 09:00:00.000     09  1

The table excludes a lot of rows because I don't think they pertain to the question, just ask if you need to see them.

As of right now every row is still displayed no matter the what shift it currently is. How can I fix this to display on the current shift?


Solution

  • get the current hour, if that hour is in the day shift then I want to display only the day shift rows. If that hour is in the night shift then I want to display only the night shift rows.

    As I understand the question, you just need to apply the same logic you used to compute column WhichShift to the current date. That would be (with a little simplification on your case expression):

    with data as (...)
    select *
    from data
    where whichShift = case when datepart(hour, getdate()) between 7 and 18 then 1 else 0 end
    

    When the current hour of the day is between 7 and 18, the case expression returns 1, which filters on day shifts in column whichShift (and conversely for night shifts).