2014-01-21 88 views
0

爲什麼我的查詢表現異常?我已經工作了很多,使其工作,我做了某種方式,使其工作通過stackoverflow職位等,但仍然有一些問題查詢工作異常

我想通過查找銀泰和休閒時間之間的差異來計算一個人的總工作時間,它可以工作,但對於某些記錄顯示錯誤時間

eg

InTime  TimeOut  total time 
12:00  18:11   5:11 // wrong, it should be 6:11 
13:01  18:20   4:19 // Wrong, 
09:33  17:15   7:42 // It fine and correct 

喜歡多條記錄是否能夠正常工作,但對一些人來說並不總是顯示-1的實際時間。

WITH times 
     AS (
      SELECT t1.EmplID 
       ,t3.EmplName 
       ,min(t1.RecTime) AS InTime 
       ,max(t2.RecTime) AS [TimeOut] 
       ,t1.RecDate AS [DateVisited] 
      FROM AtdRecord t1 
      INNER JOIN AtdRecord t2 ON t1.EmplID = t2.EmplID 
       AND t1.RecDate = t2.RecDate 
       AND t1.RecTime < t2.RecTime 
      INNER JOIN HrEmployee t3 ON t3.EmplID = t1.EmplID 
      GROUP BY t1.EmplID 
       ,t3.EmplName 
       ,t1.RecDate 
      ) 
     SELECT EmplID 
      ,EmplName 
      ,InTime 
      ,[TimeOut] 
      ,[DateVisited] 
      ,CASE 
       WHEN minpart = 0 
        THEN CAST(hourpart AS NVARCHAR(200)) + ':00' 
       ELSE CAST((hourpart - 1) AS NVARCHAR(200)) + ':' + CAST(minpart AS NVARCHAR(200)) 
       END AS 'total time' 
     FROM (
      SELECT EmplID 
       ,EmplName 
       ,InTime 
       ,[TimeOut] 
       ,[DateVisited] 
       ,DATEDIFF(Hour, InTime, [TimeOut]) AS hourpart 
       ,DATEDIFF(minute, InTime, [TimeOut]) % 60 AS minpart 
      FROM times 
      ) source 

這篇文章幾乎使它工作,但後來我發現了問題。 DateDiff to output hours and minutes

Example SQLFiddle

輸出:

EmplID    EmpName InTime TimeOut DateVisited Total time 
00000024   Tariq 09:59 18:56 2013-09-21 8:57 
00000024   Tariq 10:57 19:00 2013-09-23 8:3 
00000024   Tariq 11:40 18:58 2013-09-24 7:18 

回答

1
SELECT EmplID 
       ,EmplName 
       ,InTime 
       ,[TimeOut] 
       ,[DateVisited] 
       ,CASE 
        WHEN minpart = 0 
         THEN CAST(hourpart AS NVARCHAR(200)) + ':00' 
        WHEN minpart <10 
         THEN CAST(hourpart AS NVARCHAR(200)) + ':0'+ CAST(minpart AS NVARCHAR(200)) 
        ELSE CAST(hourpart AS NVARCHAR(200)) + ':' + CAST(minpart AS NVARCHAR(200)) 

END AS 'total time' 
      FROM (
       SELECT EmplID 
        ,EmplName 
        ,InTime 
        ,[TimeOut] 
        ,[DateVisited] 
        ,DATEDIFF(minute, InTime, [TimeOut])/60 AS hourpart 
        ,DATEDIFF(minute, InTime, [TimeOut]) % 60 AS minpart 
       FROM times 
       ) source 
+0

更新我的查詢部分。現在試試! – DhruvJoshi

+0

錯誤:消息156,級別15,狀態1,行36 關鍵字'FROM'附近的語法不正確。 –

+0

刪除了額外的逗號。請立即嘗試 – DhruvJoshi

1

只是採取分鐘和DATEDIFF,然後作出一個新的時間指出

with times as (
SELECT t1.EmplID 
     , t3.EmplName 
     , min(t1.RecTime) AS InTime 
     , max(t2.RecTime) AS [TimeOut] 
     , t1.RecDate AS [DateVisited] 
     , DATEDIFF(minute,min(t1.RecTime), max(t2.RecTime)) TotalMin 
FROM AtdRecord t1 
INNER JOIN 
     AtdRecord t2 
ON t1.EmplID = t2.EmplID 
AND t1.RecDate = t2.RecDate 
AND t1.RecTime < t2.RecTime 
inner join 
     HrEmployee t3 
ON t3.EmplID = t1.EmplID 
group by 
      t1.EmplID 
     , t3.EmplName 
     , t1.RecDate 
) 
SELECT EmplID 
     , EmplName 
     , InTime 
     , [TimeOut] 
     , [DateVisited] 
     , CAST((TotalMin/60) as varchar(2)) + ':' + CAST((TotalMin % 60) as varchar(2)) as [total time] 
from times 
Order By EmplID, DateVisited 
+0

錯誤:消息207,級別16,狀態1,行7 無效的列名稱'InTime'。 消息207,級別16,狀態1,行7 無效的列名'TimeOut'。 –

+0

哎呀,我的壞。我不應該使用別名。在答案中糾正了它 –