SQL:如何获取实际的移位值

时间:2017-03-28 13:39:03

标签: sql sql-server reporting-services ssms

我在SSRS中创建了一个报告,其中显示了所有的机器卷 最后一小时:

where  IntervalStartDateTime >= dateadd(HOUR, -2, GETDATE())
    And IntervalStartDateTime <= dateadd(HOUR, -1, GETDATE())

过去4小时:

where  IntervalStartDateTime >= dateadd(HOUR, -4, GETDATE())
    And IntervalStartDateTime <= dateadd(HOUR, -3, GETDATE())

从过去8小时开始:

where  IntervalStartDateTime >= dateadd(HOUR, -8, GETDATE())
    And IntervalStartDateTime <= dateadd(HOUR, -7, GETDATE())

另外我想为实际班次显示一行 - &gt;我有一些问题。 有三班倒。 夜班:从:05:45 - 13:45 早班:从13:45 - 21:45 迟班:从:21:45 - 05:45

表:

IntervalStartDateTime
2017-03-28 15:30:00.000
2017-03-28 15:30:00.000
2017-03-28 15:30:00.000
2017-03-28 15:30:00.000
2017-03-28 15:30:00.000

如何获得实际的班次值? 我正在使用Microsoft SQL Server Management Studio

2 个答案:

答案 0 :(得分:0)

目前的转变......

with WorkByShift as
(
select OtherFields,
       case
         when cast(IntervalStartDateTime as time) < '05:45:00' then 1
         when cast(IntervalStartDateTime as time) < '13:45:00' then 2
         when cast(IntervalStartDateTime as time) < '21:45:00' then 3
         else 1
       end as ShiftNo
from MyTable
)
select WBS.*
from WorkByShift  WBS
where ShiftNo = case
                 when cast(GETDATE() as time) < '05:45:00' then 1
                 when cast(GETDATE() as time) < '13:45:00' then 2
                 when cast(GETDATE() as time) < '21:45:00' then 3
                 else 1
                end
and IntervalStartDateTime > dateadd(HOUR, -16, getdate())

答案 1 :(得分:0)

请尝试 -

WHERE ( IntervalStartTime >= CASE
                                 WHEN CONVERT( TIME, GETDATE() ) < '05:45:00' THEN CONVERT( DATE, DATEADD( DAY, -1, GETDATE() ) ) + CAST( '21:45:00' AS DATETIME )
                                 WHEN CONVERT( TIME, GETDATE() ) < '13:45:00' THEN CONVERT( DATE, GETDATE() ) + CAST( '05:45:00' AS DATETIME )
                                 WHEN CONVERT( TIME, GETDATE() ) < '21:45:00' THEN CONVERT( DATE, GETDATE() ) + CAST( '13:45:00' AS DATETIME )
                                 ELSE                                              CONVERT( DATE, GETDATE() ) + CAST( '21:45:00' AS DATETIME )
                             END )

注意:这是我第一次尝试使用SQL-Server,但我想在某个时候开始使用它。 :)

如果您有任何问题或意见,请随时发表评论。

进一步阅读......

combining Date and Time fields to DateTime, SQL Server 2008

How to return the date part only from a SQL Server datetime datatype