Pengaturan Skema MS SQL Server 2008 :
CREATE TABLE Table2
([repID] int, [ClockIn] datetime, [ClockOut] datetime, [TotalHours] varchar(7))
;
INSERT INTO Table2
([repID], [ClockIn], [ClockOut], [TotalHours])
VALUES
(109145, '7:50:50 AM', '3:37:16 PM', '7:46:26'),
(109145, '7:52:41 AM', '3:44:51 PM', '7:52:10'),
(109145, '8:42:40 AM', '3:46:29 PM', '7:3:49'),
(109145, '7:50:52 AM', '3:42:59 PM', '7:52:7'),
(109145, '8:09:23 AM', '3:36:55 PM', '7:27:32')
;
Kueri 1 :
SELECT convert(varchar(8), dateadd(second, SUM(DATEDIFF(SECOND, ClockIn, ClockOut)), 0), 108)
from Table2
group by repID
Hasil :
| COLUMN_0 |
------------
| 14:02:04 |
Kueri 2 :
select sum(datediff(second,ClockIn,ClockOut))/3600 as hours_worked
from Table2
Hasil :
| hours_worked|
------------
| 38 |
Kueri 3 :
select sum(datediff(minute, 0, TotalHours)) / 60.0 as hours_worked
from Table2
Hasil :
| HOURS_WORKED |
----------------
| 38 |
Di sini pertanyaan terakhir diambil dari jawaban FreeLancer karena saya ingin tahu apakah itu berhasil atau tidak.
Di sini, Pertama, Anda perlu mengubah perbedaan waktu-tanggal menjadi detik atau menit, lalu mengubah waktu itu kembali ke jam.
Semoga membantu.