2012-01-18 56 views
1

我不確定是否有可能做到我想要的,但希望這裏有人能給我一些方向。我正在嘗試做一些勞工報告,其中一些需要我按小時進行比較。查詢計算部分人工小時

從儘可能簡單的開始,我想知道如何查詢給定小時的員工進出時間,並查明總共有多少小時工作。

舉例來說,如果我有:

id In   Out 
-- -------  ------ 
1 12:00pm  8:30pm 
2 12:00pm  8:15pm 
3  8:15pm  11:00pm 

,我想查詢如何勞動的許多分,在8點鐘小時,然後我希望看到2小時的結果(或120分鐘...... 30m + 15m + 45m)。

然後從那裏繼續我想寫一個查詢,將顯示這一整天的信息,但小時顯示每小時相同的信息。我意識到還有其他問題(即我還需要在日期上進行分組等),但是如果我能夠弄清楚這一點,那麼其餘部分將變得輕而易舉。

更多情況下,你可以看到一個類似的問題,我這裏有:Query for labor cost by hour

(這個問題和其他之間的區別,如果它是不明確的,是,我問一個具體的做法在這裏。如果你認爲你有更好的方法來解決這個問題,那麼請將它添加到其他問題中。)

+3

是否有日期,或只是時間? IN和OUT可以跨越日期邊界嗎? I.E. IN在11:30 PM,OUT在2:30 AM? – Sparky 2012-01-18 17:00:34

+0

他們會交叉,但爲了簡單起見,讓我們假設他們現在不會。在我得到如何做到這一點之後,我可以照顧邊緣案例。 – 2012-01-18 18:02:05

回答

1

試試這個讓你開始,需要調整一下,但是應該給你你想要的樣本數據...

select id, 
minutes - case when inMinutes < 0 then 0 else inminutes end as TotalMins 
from 
(
select id, 
case when datediff(mi,'8:00pm',OutTime) >60 then 60 else datediff(mi,'8:00pm',OutTime) end as Minutes, 
case when datediff(mi,'8:00pm',InTime) >60 then 60 else datediff(mi,'8:00pm',InTime) end as InMinutes 
from testhours 
) xx 
+0

datediff(mi,'8:00 pm'OutTime)總是會超過60,因爲它是從時間開始計算的(正如sql知道的那樣)+ 8小時。否則,我認爲你在正確的軌道上。我只是在解決日期問題的最佳方法上打開了另一個問題,如果你關心它:http://stackoverflow.com/questions/8915364/most-concise-way-to-write-this-query-to -add-hour-to-date-date – 2012-01-18 18:32:57

+0

將此標記爲答案,因爲這裏是最好的答案,我可以走這個方向(儘管你的答案需要調整)。但我最終做了一點不同:http://stackoverflow.com/questions/8916447/calculate-how-many-minutes-used-in-a-given-hour – 2012-01-19 20:56:38

0

如果是一個Microsoft系統然後日期時間值可以被轉換爲雙精度浮點其中小數點劃分這樣的日期和時間點數字:

  • 整數部分:天數,因爲1900-01-01
  • 小數部分:時間在一天(使0.5 = 12:00:00)

所以,你可以使用這樣的事情:

-- sample datetime values 
declare @in datetime, @out datetime 
set @in = '2012-01-18 12:00:00' 
set @out = '2012-01-18 20:30:00' 

-- calculation 
declare @r_in real, @r_out real 
set @r_in = cast(@in as real) 
set @r_out = cast(@out as real) 

select (24 * (@r_out - @r_in)) as [hours] 

,但它是不是準確,所以我會推薦這對於計算:

select cast(datediff(second, @in, @out) as real)/3600 as [hours] 
+0

謝謝,但你不完全理解問題尚未解決...員工可以在給定的小時之前/期間/之後進/出。如何計算他們在那一小時工作了多少分鐘?我認爲Sparky是在正確的軌道上...... – 2012-01-18 18:07:37

+0

好的,我明白了。我可以假設進出時間的字段是日期時間嗎? – 2012-01-20 21:03:26

+0

是的,他們是日期時間的,但我已經得到了我的問題的解決方案已經:http://stackoverflow.com/questions/8916447/calculate-how-many-minutes-used-in-a-given-hour – 2012-01-20 21:18:22

0

OK,我也建議使用的功能,因爲這也許慢了很多大表上也更容易,更簡單的代碼。

但這裏是沒有功能的另一個解決方案:

-- the day you're interested in: 
declare @day datetime 
set @day = '2012-01-20' 

-- sample data 
declare @moves table (id int, tin datetime, tout datetime) 
insert into @moves values 
    (1, '2012-01-20 06:30:00', '2012-01-20 15:45:00'), 
    (2, '2012-01-20 13:05:00', '2012-01-20 19:45:00'), 
    (3, '2012-01-20 10:10:00', '2012-01-20 10:50:00'), 
    (4, '2012-01-20 19:35:00', '2012-01-20 21:00:00') 

-- a helper table with hours 
declare @i int 
declare @hours table (h int, f datetime, t datetime) 
set @i = 0 
while @i < 24 
    begin 
    insert into @hours values(@i, dateadd(hour, @i, @day), dateadd(hour, @i + 1, @day)) 
    set @i = @i + 1 
    end 

-- here's the code 
select h.h, 
    sum(case sign(datediff(second, h.f, m.tin)) 
     when 1 then 
      case sign(datediff(second, m.tout, h.t)) 
       when 1 then datediff(minute, m.tin , m.tout) 
       else datediff(minute, m.tin , h.t) 
       end 
     when null then null 
     else 
      case sign(datediff(second, m.tout, h.t)) 
       when 1 then datediff(minute, h.f, m.tout) 
       else datediff(minute, h.f, h.t) 
       end 
     end) as minutesWorked, 
     count(distinct m.id) as peopleWorking 
    from @hours h inner join @moves m 
    -- on h.f >= m.tin and h.t <= m.tout 
    on h.f <= m.tout and h.t >= m.tin 
group by h.h 
order by h.h 

這會給你下面的結果:

h   minutesWorked peopleWorking 
----------- ------------- ------------- 
6   30   1 
7   60   1 
8   60   1 
9   60   1 
10   100   2 
11   60   1 
12   60   1 
13   115   2 
14   120   2 
15   105   2 
16   60   1 
17   60   1 
18   60   1 
19   70   2 
20   60   1 
21   0    1