我正在使用此查询,结果如下:
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)
您应该在左侧查询中有两个查询总和小时数,在右侧选择所需的列然后加入查询。
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