[英]Query to calculate sum of time with above 24 hours interval
I have time like 25:00:00, 10:00:00, 30:00:00 and I want to calculate the sum of the values from database.我有 25:00:00、10:00:00、30:00:00 这样的时间,我想计算数据库中值的总和。 How can I do that the current query for calculating sum is
我该怎么做,当前计算总和的查询是
SQL query: SQL查询:
SELECT
CAST(FORMAT((SUM((DATEPART("ss", Total_Time) +
DATEPART("mi", Total_Time) * 60 +
DATEPART("hh", Total_Time) * 3600)) / 3600), '00') AS varchar(max)) + ':' + CAST(FORMAT((SUM((DATEPART("ss", Total_Time) + DATEPART("mi", Total_Time) * 60 + DATEPART("hh", Total_Time) * 3600)) % 3600 / 60), '00') as varchar(max)) + ':' +CAST(FORMAT((SUM((DATEPART("ss", Total_Time) + DATEPART("mi", Total_Time) * 60 + DATEPART("hh", Total_Time) * 3600)) % 3600 % 60), '00') as varchar(max)) as WorkingTimeSum
FROM
tasker_usr.TaskTime
but I get an error但我得到一个错误
Cannot convert from datetime to character string
无法从日期时间转换为字符串
You cannot fit your values into a time
datatype.您不能将您的值放入
time
数据类型中。 It stores only value less than 23:59:59.9999999
.它仅存储小于
23:59:59.9999999
的值。 Hence you cannot use DATEPART
or DATEDIFF
directly.因此,您不能直接使用
DATEPART
或DATEDIFF
。 One way is to split your values( as seconds ) and do the sum.一种方法是拆分您的值(如秒)并求和。 Something like
就像是
WITH CTE_SUM
AS (
SELECT SUM(LEFT(column_name, 2) * 3600
+ SUBSTRING(column_name, 4, 2) * 60
+ SUBSTRING(column_name, 7, 2)) AS SUM_AS_SECONDS
FROM YOUR_TABLE
)
SELECT CAST(SUM_AS_SECONDS / 3600 AS VARCHAR) + ':'
+ CAST(SUM_AS_SECONDS % 3600 / 60 AS VARCHAR) + ':'
+ CAST(((SUM_AS_SECONDS % 3600) % 60) AS VARCHAR) AS WorkingTimeSum
FROM CTE_SUM
Please note that this will work upto 99:99:99
.If you have time values that are more than that, then you have to split based on :
instead of hardcoding values in LEFT/Substring
请注意,这将在
99:99:99
。如果您的时间值超过此值,则必须根据:
而不是LEFT/Substring
中的硬编码值进行拆分
Are you looking something like this?你在寻找这样的东西吗?
Declare @Hours Table(Hrs VARCHAR(20))
Declare @Hour TINYINT, @Minute INT,@Second INT, @InSeconds BIGINT, @Cdate DATETIME
INSERT @Hours
SELECT '25:00:00' Union All
SELECT '10:00:00' Union All
SELECT '30:00:00'
select @Hour = sum(cast(Left(Hrs,CharIndex(':',Hrs,1)-1) as int)) from @Hours
select @Minute = sum(cast(Substring(Hrs,CharIndex(':',Hrs,1)+1,2)as int)) from @Hours
select @Second = sum(cast(Substring(Hrs,CharIndex(':',Hrs,1)+4,2)as int)) from @Hours
Set @InSeconds = @Second + (@Minute * 60) + (@Hour *60 *60)
Set @Cdate = dateadd(second,@InSeconds,0)
select datediff(day,'1900-01-01',@Cdate) [Day(s)],
Format(@Cdate,'hh') [Hour(s)],
Format(@Cdate,'mm') [Minute(s)],
Format(@Cdate,'ss') [Second(s)]
Day(s)![]() |
Hour(s)![]() |
Minute(s)![]() |
Second(s)![]() |
---|---|---|---|
2 ![]() |
05 ![]() |
00 ![]() |
00 ![]() |
Assuming that your format is HH(n):MM:SS (that is, hours can have 1 or more digits), you can decompose the value and reconstruct it.假设您的格式是 HH(n):MM:SS(即小时可以有 1 个或多个数字),您可以分解该值并重新构建它。 This requires string functions.
这需要字符串函数。 You can get the total seconds as:
您可以获得总秒数:
select sum(left(total_time, charindex(':', total_time) - 1) * 60 * 60 +
left(right(total_time, 5), 2) * 60 +
right(total_time, 2)
) as total_secs
from tasker_usr.TaskTime;
Note: This does implicit conversion from the string values to integers, but that should be okay if your values are consistent.注意:这会从字符串值隐式转换为整数,但如果您的值一致,那应该没问题。
You can convert back to string:您可以转换回字符串:
select concat(format(total_secs / (60 * 60), '00:'),
format( (total_secs / 60) % 60, '00:'),
format(total_secs % 60, '00')
) as time_format
from (select sum(left(total_time, charindex(':', total_time) - 1) * 60 * 60 +
left(right(total_time, 5), 2) * 60 +
right(total_time, 2)
) as total_secs
from tasker_usr.TaskTime
) t
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.