1

我有这样的桌子

Id  Student     Test_Start          Test_End
1   A       2013-01-08 15:00:17.100     2013-01-08 16:05:55.100 
2   B       2013-02-08 17:01:11.800     2013-02-08 19:05:15.880
3   C       2013-02-08 14:41:10.880     2013-02-08 16:49:22.880

我想要做的是获取 Test_End - Test_Start 时间并将其更改为小时并按升序显示。我试过了:

Select convert(date, Test_End) - convert(date,Test_Start) as 'Time_Taken' from students 

或已尝试使用 DATEDIFF 但似乎无法使其工作。有没有最好的方法来完成这个来显示这样的东西:

Time_Taken
1
2
2
4

2 回答 2

4
select 
    id,
    student,
    datediff(hour, test_start, test_end) as time_taken
from students
order by time_taken

如果你想四舍五入,确定分钟,加上将需要四舍五入到下一个小时的分钟数,然后除以 60 得到四舍五入的小时......

select 
    id,
    student,
    (datediff(minute, test_start, test_end) + 30) / 60 as rounded_time_taken
from students
order by rounded_time_taken
于 2013-01-08T22:17:43.893 回答
1

你真的想要整数小时吗?在大多数情况下,我宁愿将结果视为分数小时:

select id, student, datediff(s, test_start, test_end)/(60.0*60.0) as hours_taken
from students
order by test_start, test_end)/(60.0*60.0) 
于 2013-01-08T22:28:49.997 回答