SQL似乎在select语句中自动舍入数字?

时间:2013-02-20 01:50:23

标签: sql

您好我的SQL代码:

    SELECT a."Date", a."Missed", b."Total Client Schedules", cast(100-((a."Missed"*100) / b."Total Client Schedules")AS decimal) as "Pct Completed" -
  FROM -
   ( -
     SELECT DATE(scheduled_start) as "Date",count(*) as "Missed" FROM -
     events WHERE node_name IS NOT NULL AND status IN ('Missed') GROUP BY DATE(scheduled_start) -
    ) as a, -
   ( -
    SELECT DATE(scheduled_start) as "Date", count(*) as -
    "Total Client Schedules" FROM events WHERE node_name IS NOT NULL GROUP BY DATE(scheduled_start) -
    ) as b -
  WHERE a."Date" = b."Date" ORDER BY "Date" desc

,这是输出

       Date           Missed      Total Client Schedules      Pct Completed
-----------     ------------     -----------------------     --------------
 2013-02-20                2                         805                100
 2013-02-19               14                         805                 99
 2013-02-18               29                         805                 97
 2013-02-17               59                         805                 93
 2013-02-16               29                         806                 97
 2013-02-15               49                         805                 94
 2013-02-14               33                         805                 96
 2013-02-13               57                         805                 93
 2013-02-12               21                         805                 98
 2013-02-11               35                         805                 96
 2013-02-10               34                         805                 96

当我希望它达到99.99%或97.2%等时,它总是似乎是最高的数字。

1 个答案:

答案 0 :(得分:0)

您没有指定您正在使用的数据库。但是,有些数据库会进行整数运算,因此1/2为0而不是0.5。

要解决此问题,只需使用数字而不是整数来构造常量:

cast(100.0-((a."Missed"*100.0) / b."Total Client Schedules")AS decimal)

然后它将转换为算术的非整数类型。