2014-05-14 48 views
1

我使用這個查詢和它的結果是:添加列和商店中的所有行的另一列

SELECT  
    t.TestId, 
    t.Days, 
    t.FullName 
    [Date] = Convert(date,DATEADD(dd, 0, DATEDIFF(dd, 0, t.CheckIn))), 
    CheckIn = CONVERT(CHAR(5), t.CheckIn, 108), 
    CheckOut = CONVERT(CHAR(5), t.CheckOut, 108), 
    [Hours] = CAST(DATEDIFF(MINUTE, t.CheckIn, t.CheckOut)/60. AS DECIMAL(10,2)) 
FROM (
    SELECT 
    FullName = Users.FullName, 
    TestId = t.TestId, 
    Days = t.Days, 
    t.UserId_Fk, 
    CheckIn = t.CheckInTime, 
    CheckOut = r.CheckInTime, 
    RowNum = ROW_NUMBER() OVER (PARTITION BY t.UserId_Fk, r.CheckInTime ORDER BY 1/0) 
FROM UserTime t 
INNER JOIN Users 
ON t.UserId_Fk=Users.UserId 
    OUTER APPLY (
     SELECT TOP 1 * 
     FROM UserTime t2 
     WHERE 
     t2.CheckInTime > t.CheckInTime 
     AND DATEADD(dd, 0, DATEDIFF(dd, 0, t.CheckInTime)) = DATEADD(dd, 0, DATEDIFF(dd, 0, t2.CheckInTime)) 
     AND t2.LoginStatus = 'O' 
     ORDER BY t2.CheckInTime 
) r 
WHERE t.LoginStatus = 'I' 
) t 
WHERE t.RowNum = 1 

結果:

TestId Days FullName  Date CheckIn CheckOut Hours 
11 Wednesday Antonio 2014-05-14 10:19 10:20 0.02 
13 Wednesday Antonio 2014-05-14 10:19 10:20 0.02 
14 Wednesday Tim 2014-05-14 10:20 10:21 0.02 

表結構:

表用戶時間:

TestId int(pk)  
UserId_Fk int  
Days  nvarchar(50) 
Date   date 
CheckInTime datetime 
LoginStatus char(1)  

表用戶:

UserId int(Pk) 
FullName varchar(50) 

我想要一個名爲TotalHours列其中添加時間列的所有領域,並顯示該值。

有點像這樣:

TestId Days  FullName Date CheckIn CheckOut Hours TotalHours 
11 Wednesday Antonio 2014-05-14 10:19 10:20 0.02 0.04 
11 Wednesday Antonio 2014-05-14 10:19 10:20 0.02 
13 Wednesday Tim 2014-05-14 10:20 10:21 0.02 
+0

一次或每一行? –

+0

抱歉不清楚。根據用戶。所以根據UserId_Fk添加時間列。 – user3590485

回答

0

你應該在左邊查詢和兩個查詢的總時間和正確的選擇,則所需的列加入查詢。

select * from 
    (SELECT testid,sum(Hours) as total from(
     SELECT  
     t.TestId, 
     t.FullName , 
     [Hours] = CAST(DATEDIFF(MINUTE, t.CheckIn, t.CheckOut)/60. AS DECIMAL(10,2)) 

     FROM (
      SELECT 
      FullName = Users.FullName, 
      TestId = t.TestId, 
      Days = t.Days, 
      t.UserId_Fk, 
      CheckIn = t.CheckInTime, 
      CheckOut = r.CheckInTime, 
      RowNum = ROW_NUMBER() OVER (PARTITION BY t.UserId_Fk, r.CheckInTime ORDER BY 1/0) 
      FROM UserTime t 
      INNER JOIN Users 
      ON t.UserId_Fk=Users.UserId 
      OUTER APPLY (
      SELECT TOP 1 * 
      FROM UserTime t2 
      WHERE 
      t2.CheckInTime > t.CheckInTime 
      AND DATEADD(dd, 0, DATEDIFF(dd, 0, t.CheckInTime)) = DATEADD(dd, 0, DATEDIFF(dd, 0, t2.CheckInTime)) 
     AND t2.LoginStatus = 'O' 
    ORDER BY t2.CheckInTime 
) r 
    WHERE t.LoginStatus = 'I' 
) t 
    WHERE t.RowNum = 1 
)s 
    group by s.FullName,testid)O 


    INNER JOIN 

    (
    SELECT  
    t.TestId, 
    t.Days, 
    t.FullName , 
    [Date] = Convert(date,DATEADD(dd, 0, DATEDIFF(dd, 0, t.CheckIn))), 
    CheckIn = CONVERT(CHAR(5), t.CheckIn, 108), 
    CheckOut = CONVERT(CHAR(5), t.CheckOut, 108), 
    [Hours] = CAST(DATEDIFF(MINUTE, t.CheckIn, t.CheckOut)/60. AS DECIMAL(10,2)) 

    FROM (
     SELECT 
     FullName = Users.FullName, 
     TestId = t.TestId, 
     Days = t.Days, 
     t.UserId_Fk, 
     CheckIn = t.CheckInTime, 
     CheckOut = r.CheckInTime, 
     RowNum = ROW_NUMBER() OVER (PARTITION BY t.UserId_Fk, r.CheckInTime ORDER BY 1/0) 
     FROM UserTime t 
     INNER JOIN Users 
     ON t.UserId_Fk=Users.UserId 
     OUTER APPLY (
     SELECT TOP 1 * 
    FROM UserTime t2 
    WHERE 
    t2.CheckInTime > t.CheckInTime 
    AND DATEADD(dd, 0, DATEDIFF(dd, 0, t.CheckInTime)) = DATEADD(dd, 0, DATEDIFF(dd, 0, t2.CheckInTime)) 
    AND t2.LoginStatus = 'O' 
    ORDER BY t2.CheckInTime 
) r 
WHERE t.LoginStatus = 'I' 
) t 
WHERE t.RowNum = 1 
)I 
on i.testid=o.testid