2012-11-14 17 views
5

有沒有簡單的方法來做到這一點?完全之間,我的意思是不計算上午7點或下午7點的日期時間等於開始或結束時間。計算兩個日期時間之間7AM或7PM事件的數量

我想這可以用秒和一點代數unix時間戳完成,但我無法弄清楚。

我很高興在PLSQL或純SQL中使用某些東西。

例子:

start    end    num_7am_7pm_between_dates 
2012-06-16 05:00 2012-06-16 08:00 1 
2012-06-16 16:00 2012-06-16 20:00 1 
2012-06-16 05:00 2012-06-16 07:00 0 
2012-06-16 07:00 2012-06-16 19:00 0 
2012-06-16 08:00 2012-06-16 15:00 0 
2012-06-16 05:00 2012-06-16 19:01 2 
2012-06-16 05:00 2012-06-18 20:00 6 

回答

3

我覺得這可以進一步減少,但我並不在我手上完全測試此的Oracle SQL有甲骨文:

SELECT StartDate 
    , EndDate 
    , CASE WHEN TRUNC(EndDate) - TRUNC(StartDate) < 1 
      AND TO_CHAR(EndDate, 'HH24') > 19 
      AND TO_CHAR(StartDate, 'HH24') < 7 
      THEN 2 
      WHEN TRUNC(EndDate) - TRUNC(StartDate) < 1 
      AND (TO_CHAR(EndDate, 'HH24') > 19 
       OR TO_CHAR(StartDate, 'HH24') < 7) 
      THEN 1 
      WHEN TRUNC(EndDate) - TRUNC(StartDate) > 0 
      AND TO_CHAR(EndDate, 'HH24') > 19 
      AND TO_CHAR(StartDate, 'HH24') < 7 
      THEN 2 + ((TRUNC(EndDate) - TRUNC(StartDate)) * 2) 
      WHEN TRUNC(EndDate) - TRUNC(StartDate) > 0 
      AND TO_CHAR(EndDate, 'HH24') > 19 
       OR TO_CHAR(StartDate, 'HH24') < 7 
      THEN 1 + ((TRUNC(EndDate) - TRUNC(StartDate)) * 2) 
      ELSE 0 
     END 
FROM MyTable; 

感謝@ABCade的小提琴,它看起來像我的情況下邏輯可以濃縮進一步:

SELECT SDate 
    , EDate 
    , CASE WHEN TO_CHAR(EDate, 'HH24') > 19 
      AND TO_CHAR(SDate, 'HH24') < 7 
      THEN 2 + ((TRUNC(EDate) - TRUNC(SDate)) * 2) 
      WHEN TO_CHAR(EDate, 'HH24') > 19 
       OR TO_CHAR(SDate, 'HH24') < 7 
      THEN 1 + ((TRUNC(EDate) - TRUNC(SDate)) * 2) 
      ELSE 0 
     END AS MyCalc2 
    FROM MyTable; 
+1

看起來你是正確的 - 你可以使用這個小提琴http://www.sqlfiddle.com/#!4/baead/2 –

+0

謝謝你的小提琴。現在看看。 –

+0

還不好。 [http://www.sqlfiddle.com/#!4/baead/6](http://www.sqlfiddle.com/#!4/baead/6)。首先,當兩個小時數小於7(應該爲零)或兩者都大於19時,計數爲1.其次,不考慮分鐘數。例如,不計數19:01。 –

2

這可能不會有最好的表現,但你可能工作:

select sdate, edate, count(*) 
    from (select distinct edate, sdate, sdate + (level/24) hr 
      from t 
     connect by sdate + (level/24) <= edate) 
where to_char(hr, 'hh') = '07' 
group by sdate, edate 

更新:對於@ FlorinGhita的評論 - 固定查詢包含零發生

select sdate, edate, sum(decode(to_char(hr, 'hh'), '07',1,0)) 
    from (select distinct edate, sdate, sdate + (level/24) hr 
      from t 
     connect by sdate + (level/24) <= edate) 
group by sdate, edate 
+0

將排除行零個OCCURENCES –

+0

@FlorinGhita,謝謝你,我的固定查詢 –

+0

+1我認爲現在是確定的。 –

1

做這樣的(在SQL)

declare @table table (start datetime, ends datetime) 
insert into @table select'2012-06-16 05:00','2012-06-16 08:00' --1 
insert into @table select'2012-06-16 16:00','2012-06-16 20:00' --1 
insert into @table select'2012-06-16 05:00','2012-06-16 07:00' --0 
insert into @table select'2012-06-16 07:00','2012-06-16 19:00' --0 
insert into @table select'2012-06-16 08:00','2012-06-16 15:00' --0 
insert into @table select'2012-06-16 05:00','2012-06-16 19:01' --2 
insert into @table select'2012-06-16 05:00','2012-06-18 20:00' --6 
insert into @table select'2012-06-16 07:00','2012-06-18 07:00' --3 


Declare @From DATETIME 
Declare @To DATETIME 

select @From = MIN(start) from @table 
select @To = max(ends) from @table 

;with CTE AS 
( 
     SELECT distinct 
    DATEADD(DD,DATEDIFF(D,0,start),0)+'07:00' AS AimTime 
    FROM @table 
),CTE1 AS 
( 
    Select AimTime 
    FROM CTE 

    UNION ALL 

    Select DATEADD(hour, 12, AimTime) 
From CTE1 
WHERE AimTime< @To 
) 


select start,ends, count(AimTime) 
from CTE1 right join @table t 
on t.start < CTE1.AimTime and t.ends > CTE1.AimTime 
group by start,ends 
3

我很開心寫了以下解決方案:

with date_range as (
    select min(sdate) as sdate, max(edate) as edate 
    from t 
), 
all_dates as (
    select sdate + (level-1)/24 as hour 
    from date_range 
    connect by level <= (edate-sdate) * 24 + 1 
), 
counts as (
    select t.id, count(*) as c 
    from all_dates, t 
    where to_char(hour, 'HH') = '07' 
    and hour > t.sdate and hour < t.edate 
    group by t.id 
) 
select t.sdate, t.edate, nvl(counts.c, 0) 
from t, counts 
where t.id = counts.id(+) 
order by t.id; 

如果日期範圍不唯一,我在表中添加了一個id列。

http://www.sqlfiddle.com/#!4/5fa19/13

相關問題