2015-11-17 58 views
0

我有一個時間框架:分類間隔日夜SQL服務器

FROMTIME:06:01:00 - TOTIME:23:59:00 = DAY 
FROMTIME:23:59:01 - TOTIME:06:00:00 = Night 

我有一輛車TIME_IN和TIME_OUT。 如何分類汽車間隔時間是DAY或Night或DayAndNight 。我使用的代碼,但它的執行很長一段時間:

Declare @From_Time_Day time 
 
\t \t ,@To_Time_Day time 
 
\t \t ,@From_Time_Night time 
 
\t \t ,@To_Time_Night time 
 
\t \t ,@Midnight time 
 
\t \t 
 
set @From_Time_Day = (select FROM_TIME from DAY_STATUS where DAY_CHECK=1) 
 
set @To_Time_Day = (select TO_TIME from DAY_STATUS where DAY_CHECK=1) 
 
set @From_Time_Night = (select FROM_TIME from DAY_STATUS where DAY_CHECK=2) 
 
set @To_Time_Night = (select TO_TIME from DAY_STATUS where DAY_CHECK=2) 
 
set @Midnight = '00:00:00' 
 

 
select * from (
 
select 
 
\t (case when ( 
 
\t \t \t \t cast((select top 1 IO_TIME from IO_INFO where IO_STATUS= 'IN' and CA_ID = Data.CA_ID and IO_ID < Data.IO_ID order by IO_ID desc) as date) 
 
\t \t \t \t = cast(Data.IO_TIME as date) 
 
\t \t \t \t) 
 
\t \t then 
 
\t \t \t \t (
 
\t \t \t \t case when (
 
\t \t \t \t \t \t cast((select top 1 IO_TIME from IO_INFO where IO_STATUS= 'IN' and CA_ID = Data.CA_ID and IO_ID < Data.IO_ID order by IO_ID desc) as time) >= @From_Time_Day 
 
\t \t \t \t \t \t and cast(Data.IO_TIME as time) <[email protected]_Time_Day 
 
\t \t \t \t   ) then 'DAY'  
 
\t \t \t \t  when (
 
\t \t \t \t   cast((select top 1 IO_TIME from IO_INFO where IO_STATUS= 'IN' and CA_ID = Data.CA_ID and IO_ID < Data.IO_ID order by IO_ID desc) as time) >= @From_Time_Night 
 
\t \t \t \t   and cast(Data.IO_TIME as time) < @Midnight 
 
\t \t \t \t \t \t ) then 'Night' 
 
\t \t \t \t \t when (
 
\t \t \t \t \t  cast((select top 1 IO_TIME from IO_INFO where IO_STATUS= 'IN' and CA_ID = Data.CA_ID and IO_ID < Data.IO_ID order by IO_ID desc) as time) >= @Midnight 
 
\t \t \t \t \t \t and cast(Data.IO_TIME as time) <= @To_Time_Night 
 
\t \t \t \t \t  ) then 'Night' 
 
\t \t \t \t \t else 'DayAndNight' end 
 
\t \t \t \t) 
 
\t \t when (
 
\t \t \t \t cast((select top 1 IO_TIME from IO_INFO where IO_STATUS= 'IN' and CA_ID = Data.CA_ID and IO_ID < Data.IO_ID order by IO_ID desc) as date) 
 
\t \t \t \t <> cast(Data.IO_TIME as date) 
 
\t \t \t \t) 
 
\t \t then \t (
 
\t \t \t \t case when(
 
\t \t \t \t \t \t (cast((select top 1 IO_TIME from IO_INFO where IO_STATUS= 'IN' and CA_ID = Data.CA_ID and IO_ID < Data.IO_ID order by IO_ID desc) as time) >= @From_Time_Night and 
 
\t \t \t \t \t \t cast((select top 1 IO_TIME from IO_INFO where IO_STATUS= 'IN' and CA_ID = Data.CA_ID and IO_ID < Data.IO_ID order by IO_ID desc) as time) < @Midnight) 
 
\t \t \t \t \t \t and (cast(Data.IO_TIME as time)>[email protected] and cast(Data.IO_TIME as time)<[email protected]_Time_Night) 
 
\t \t \t \t   ) then 'Night' 
 
\t \t \t \t  else 'DayAndNight' end 
 
\t \t  ) 
 
\t \t end 
 
) as INTERVAL 
 
from IO_INFO as Data where IO_STATUS = 'OUT'

回答

0

好吧,看,這是我已經失去了一些時間來找到一個解決方案查詢的一個棘手的類型,我創建了一個在包含4個字段,ID,Car,TimeOut,TimeIn的測試數據庫中的表中,我在此表中插入了一些行以獲得示例,然後創建了一個Query,以返回我的汽車每次行駛的週期狀態。

下面是該查詢:

SELECT [ID] 
    ,[Car] 
    ,[TimeOut] 
    ,[TimeIn] 
    , MultiDay 
    ,OutDAyNight 
    ,InDAyNight 
    , CASE WHEN Multiday = 1 THEN 'Day/Night' 
     WHEN OutDAyNight+InDAyNight = 1 THEN 'Day/Night' 
     WHEN OutDAyNight+InDAyNight = 2 THEN 'Day' 
     WHEN OutDAyNight+InDAyNight = 0 THEN 'Night' END AS Periods 
FROM 
(SELECT [ID] 
      ,[Car] 
      ,[TimeOut] 
      ,[TimeIn]  
      , CASE WHEN DATEDIFF(hour, [TimeOut],[TimeIn]) > 18 
       THEN 1 ELSE 0 END AS MultiDay  
      , CASE WHEN DATEPART(hour, [TimeOut]) > 6 AND 
       DATEPART(hour, [TimeOut]) <=23 THEN 1 
       WHEN DATEPART(hour, [TimeOut]) = 6 AND 
       DATEPART(minute, [TimeOut]) > 0 THEN 1 
       ELSE 0 END AS OutDAyNight  
      , CASE WHEN DATEPART(hour, [TimeIn]) > 6 AND 
       DATEPART(hour, [TimeIn]) <=23 THEN 1 
       WHEN DATEPART(hour, [TimeIn]) = 6 AND 
       DATEPART(minute, [TimeIn]) > 0 THEN 1   
       ELSE 0 END AS InDAyNight  
      FROM [dbo].[TbCarInOut]) Times 

使用這種雙重查詢,在第一選擇(在腳本下一個),我使用了DateDiff函數,看看是否退出和汽車的收益之間的時間超過18小時,這肯定意味着有白天和黑夜時間。然後,我使用Datepart函數來了解是否外出時間是Night或Day,將Night設置爲0,對於Day設置爲1,對於時間In,這樣,在第二個查詢中,我可以使用一個簡單的例子,一個總和來決定這個時間段是白天/黑夜,白天還是夜晚。 HTH