0

嗨,这是我的 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% 等时,它似乎总是四舍五入到最高数字。

4

1 回答 1

0

您没有指定您使用的数据库。然而,一些数据库做整数运算,所以 1/2 是 0 而不是 0.5。

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

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

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

于 2013-02-20T01:55:39.820 回答