Sql Query:无法从另一个表中添加列

时间:2014-05-13 23:57:12

标签: sql stored-procedures

我正在使用当前运行正确的查询:

SELECT 
 t.TestId,
 t.Days,
  t.UserId_Fk    
, [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 
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
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    UserId_Fk    Date        CheckIn  CheckOut Hours
  45       Tuesday      3        2014-05-13    11:49    11:49    0.00

我想在此结果中添加另一个字段(列)命名为 FullName ,该字段来自另一个 表用户 我尝试了各种各样的东西,但没有成功。

表关系是:

              Table Test 

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


              Table Users

        UserId  int(Pk) 
       FullName  varchar(50)    

2 个答案:

答案 0 :(得分:2)

首先加入Test&用户表然后尝试选择用户的全名。您可以通过userid_fk加入。加入后,您可以申请前1名选择&其他形成在联合表上。

如下所示:



    SELECT 
     t.TestId,
     t.Days,
      t.UserId_Fk    
    , [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 *
    FROM test
    JOIN users
    ON test.userid_fk=users.userid) t

    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

如果有任何拼写错误,请尝试几次并修复查询。

答案 1 :(得分:0)

 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