我的sql表中有两个bit
类型的列,其中的值类似于,
FirstHalfLeave SecondHalfLeave
------------ -------------
0 1
1 1
我需要将这两个字段相加以使其离开一天,我需要显示确切的结果(3/2=1.5)
我刚刚将这些bit
转换为integer
,如
sum(CAST(StaffAttendance.FirstHalfStatus as Integer) +
CAST(StaffAttendance.SecondHalfStatus as integer))/2 as TotalLeave
它将结果显示为1
而不是1.5
,为此我认为我需要将其转换为加倍或浮动,我不知道如何做到这一点,任何人都可以帮助我,感谢预先
答案 0 :(得分:16)
使用/2.0
代替强制小数类型
删除转换为float
的尾随零。我认为你错了)
。尝试:
select cast(
sum(
(
CAST(StaffAttendance.FirstHalfStatus as Integer) +
CAST(StaffAttendance.SecondHalfStatus as integer)
)
/ 2.0
) as float
) as TotalLeave
from your_table
答案 1 :(得分:5)
转换为double的最简单方法是乘以1.0,如下所示:
1.0 * (sum(CAST(StaffAttendance.FirstHalfStatus as Integer) +
CAST(StaffAttendance.SecondHalfStatus as integer)))/2 as TotalLeave
答案 2 :(得分:2)
除以2.0
或乘以0.5
。
答案 3 :(得分:2)
您是否尝试过投射位列并浮动而不是整数?
sum(CAST(StaffAttendance.FirstHalfStatus as float) +
CAST(StaffAttendance.SecondHalfStatus as float))/2 as TotalLeave
答案 4 :(得分:1)
试
Sum(Case Cast(FirstHalfLeave + SecondHalfLeave as Numeric)) / Count(*)