12

我的 sql 表中有两个bit类型列,其中的值就像,

FirstHalfLeave         SecondHalfLeave
 ------------           -------------
      0                      1
      1                      1

我需要对这两个字段求和以使其离开一天,我需要显示确切的结果(3/2=1.5)

我只是将这些转换bitinteger喜欢

sum(CAST(StaffAttendance.FirstHalfStatus as Integer) + 
             CAST(StaffAttendance.SecondHalfStatus as integer))/2 as TotalLeave

它显示结果1不是1.5,为此我认为我需要将其转换为双倍或浮动,我不知道如何执行此操作,有人可以在这里帮助我,提前谢谢

4

5 回答 5

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
于 2012-05-18T12:52:58.517 回答
6

转换为 double 的最简单方法是乘以 1.0,如下所示:

   1.0 * (sum(CAST(StaffAttendance.FirstHalfStatus as Integer) + 
         CAST(StaffAttendance.SecondHalfStatus as integer)))/2 as TotalLeave
于 2012-05-18T12:52:52.463 回答
3

除以2.0或乘以0.5

于 2012-05-18T12:53:55.303 回答
2

您是否尝试过铸造位列和浮点数而不是整数?

sum(CAST(StaffAttendance.FirstHalfStatus as float) + 
             CAST(StaffAttendance.SecondHalfStatus as float))/2 as TotalLeave
于 2012-05-18T12:54:20.367 回答
1

尝试

 Sum(Case Cast(FirstHalfLeave + SecondHalfLeave as Numeric)) / Count(*)
于 2012-05-18T12:54:49.383 回答