如何将 int“小时” 和 Int“分钟” 转换为时间,然后获得两个不同时间的百分比?
从我正在使用的 int 列中获取总时间
Format((SUM([THOURS]) * 3600 + SUM([TMINUTES]) * 60 + SUM([TSECONDS])) / 3600,'00')+':'+Format((SUM([THOURS]) * 3600 + SUM([TMINUTES]) * 60 + SUM([TSECONDS])) % 3600/60,'00') as "Total Time"
我从上述查询中得到了完美的时光。
但是现在我希望从我的两个 int“小时” 和 Int“分钟” 列表中获取百分比计算。
我尝试过
DECLARE @MS INT = 235216
select cast(dateadd(ms, @MS, '00:00:00') AS TIME(3))
这不是我的要求。
有人有相同的想法吗?
答案 0 :(得分:0)
也许这会有所帮助:
declare @A table
( xHours int, xMinutes int, xSeconds int,
yHours int, yMinutes int, ySeconds int )
insert into @A ( xHours, xMinutes, xSeconds, yHours, yMinutes, ySeconds ) values
( 7, 23, 59, 13, 44, 51 ),
( 0, 00, 12, 1, 07, 00 );
with A as ( select *,
3600 * xHours + 60 * xMinutes + xSeconds as xTotal,
3600 * yHours + 60 * yMinutes + ySeconds as yTotal
from @A ),
B as ( select *,
cast ( DATEADD(second,xTotal,'00:00:00') as time(0)) as xTime,
cast ( DATEADD(second,yTotal,'00:00:00') as time(0)) as yTime
from A ),
C as ( select *, 100*CAST(xTotal as float)/CAST(yTotal as float) as Usage from B )
select xTotal, yTotal, xTime, yTime, Usage from C
结果:
xTotal yTotal xTime yTime Usage
26639 49491 07:23:59 13:44:51 53.8259481521893
12 4020 00:00:12 01:07:00 0.298507462686567
答案 1 :(得分:0)
将时间字段转换为分钟,然后对分钟进行数学运算。此示例使用两个变量-您将改用字段名。
Declare
@Allotted As Time = '08:00'
, @Taken As Time = '06:00'
Select Cast(DateDiff(Minute, '00:00', @Taken) As Numeric(17, 2)) / DateDiff(Minute, '00:00', @Allotted) as Percent_Of_Allotted_Time