SQL Server 2008中的小时和分钟总和

时间:2013-11-26 05:35:09

标签: sql sql-server-2008

我写了一个查询,但没有给出正确的答案。我想这样显示  5小时58分钟+7小时58分钟= 13小时56分钟(答案应13.56),但下面的查询给出了15.09

请使用任何其他解决方案回复我。(此处Effort1为5小时栏,Effort1Minutes为58分钟栏,同一Effort2为7小时栏,Effort2Minutes为58分钟专栏。

 select CAST((SUM(Effort1)*60+SUM(Effort1Minutes))/60 as float) +  
       (CAST((SUM(Effort1)*60+SUM(Effort1Minutes))% 60 as float)) / 100 +   
        CAST((SUM(Effort2)*60+SUM(Effort2Minutes))/60 as float) +  
       (CAST((SUM(Effort2)*60+SUM(Effort2Minutes))% 60 as float)) / 100               
 from TimesheetDetails

1 个答案:

答案 0 :(得分:1)

请尝试以下查询

    select CONVERT(decimal(18,2),SUM(Effort1 + Effort2)) + 
    SUM(Effort1Minutes + Effort2Minutes) / 60 + 
    CONVERT(decimal(18,2), (SUM(Effort1Minutes + Effort2Minutes) % 60) / 100.00)
from TimesheetDetails

我已经使用SQL Server 2008以及SQL Fiddle

对此查询进行了测试