2017-03-08 91 views
1

我得到了:如何根據每個empid獲得時間差並獲得存在總數50

id empid Ftime Stime這是字段。

1 01 2017-03-04 07:00:00  2017-03-04 23:45:00 
2 01 2017-03-05 05:14:12  2017-03-05 21:49:03 
3 01 2017-03-06 06:08:16  2017-03-06 22:58:13 
4 01 2017-03-07 04:12:19  2017-03-07 23:35:20 
5 01 2017-03-08 06:17:17  2017-03-08 23:48:19 
6 02 2017-03-04 06:40:11  2017-03-04 22:45:00 
7 02 2017-03-05 05:19:23  2017-03-05 22:49:03 
8 02 2017-03-06 07:08:58  2017-03-06 23:58:13 
9 02 2017-03-07 03:12:19  2017-03-07 22:35:20 
10 02 2017-03-08 07:19:12  2017-03-08 23:48:19 

如果總共= 54小時,那麼我需要4爲empid 01等;

結果我需要的是像

empid total hours 
01  10 
02  20 

類似的東西。

在此先感謝。

+0

我不按照你的邏輯,通過它到達10小時'01'和20小時'02'。 –

+0

是的它只是一個例子結果不是從上面給出的..謝謝 – redzsol

回答

1

試試這個我包括分鐘:

select empid,sum(hours)-50,(sum(mins)/60 + sum(sec)/60) mins from 
(select empid, TIMEDIFF(stime,ftime), 
SUBSTRING(TIMEDIFF(stime,ftime) FROM 1 FOR 2) as hours, 
SUBSTRING(TIMEDIFF(stime,ftime) FROM 4 FOR 2) as mins, 
SUBSTRING(TIMEDIFF(stime,ftime) FROM 7 FOR 2) as sec 
from yourtable) as a group by empid 
+0

這是工作感謝@reds – redzsol