sql中总小时数的总和

时间:2011-11-29 09:33:36

标签: sql crystal-reports

我在计算总小时数时遇到了一个小问题

我的工作时间如

Day1 - 12.23 Hours
Day2 - 11.43 Hours
Day3 - 10.18 Hours

当我计算总和时,它显示33.84

但实际结果是34.24。我如何通过SQL查询获取它?

提前致谢

3 个答案:

答案 0 :(得分:1)

我认为这应该可以解决问题:

;with hours as
(
select 12.53 as hours union all select 11.43 union all select 10.58
--select 12.23  as hours union all select 11.43 union all select 10.18 
)
,hours2 AS
(
select
floor(hours)  as rounded_hrs
,(hours % 1) * 100 as mins
FROM hours
)
SELECT 
CAST(sum(rounded_hrs) AS DECIMAL(18,2)) +
CAST(FLOOR(sum(mins) / 60) AS DECIMAL(18,2))  +
CAST((sum(mins) % 60) / 100 AS DECIMAL(18,2))
from hours2

答案 1 :(得分:0)

通常,对总计小时数的查询如下:

SELECT 
    user_id, 
    SUM(UNIX_TIMESTAMP(your_date_field))/3600 AS total_hours FROM yourTable
GROUP BY date(your_date_field), user_id;

希望有所帮助

答案 2 :(得分:0)

我认为您的工作时间实际上是整个部分的值,小时和分数是分钟。在这种情况下,您必须分别对整个零件和零件进行求和:

WITH TimePartsSplit AS (
  SELECT
    IntHours = CAST(hours AS int),
    IntMinutes = CAST((hours - FLOOR(hours)) * 100 AS int)
  FROM atable
)
SELECT
  SUM(IntHours), SUM(IntMinutes) / 60, SUM(IntMinutes) % 60 * 0.01,
  SUM(IntHours) + SUM(IntMinutes) / 60 + SUM(IntMinutes) % 60 * 0.01
FROM TimePartsSplit