0

这里我有一个有点复杂的 SQL 命令。我怎样才能把它的结果作为浮点数?

我应该把CAST和放在哪里AS FLOAT

update T1 set T1.fm_hour = T2.fm_hour
from T T1 join (Select distinct name,sum(fm) over (partition by name)/60 fm_hour
                from T) T2 on T1.name = T2.name
where T1.work_type = 'out'

非常感谢。

4

2 回答 2

2

有几种方法可以做到这一点。

您可以在更新中设置值时强制转换:

update T1 set T1.fm_hour = cast(T2.fm_hour as float)
from T T1 
join 
(
   Select distinct name, sum(fm) over (partition by name)/60 fm_hour
   from T
) T2 on T1.name = T2.name
where T1.work_type = 'out'

或者可以投60:

update T1 set T1.fm_hour = T2.fm_hour
from T T1 
join 
(
   Select distinct name, sum(fm) over (partition by name)/ cast(60 as float) fm_hour
   from T
) T2 on T1.name = T2.name
where T1.work_type = 'out'

或者你可以只除以 60.0:

update T1 set T1.fm_hour = T2.fm_hour
from T T1 
join 
(
   Select distinct name, sum(fm) over (partition by name)/ 60.0 fm_hour
   from T
) T2 on T1.name = T2.name
where T1.work_type = 'out'
于 2013-02-26T02:59:40.237 回答
1

尝试这个 :

update T1 set T1.fm_hour = T2.fm_hour
from T T1 join (Select distinct name,cast(sum(fm) over (partition by name)/60 as float) fm_hour
                from T) T2 on T1.name = T2.name
where T1.work_type = 'out'
于 2013-02-26T02:59:29.273 回答