简体   繁体   English

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

[英]SQL seems to round up the number automatically on select statement?

Hi Here is my SQL code: 嗨,这是我的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

and Here is the output 这是输出

       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

it always seems to round to the highest number when i want it to be like 99.99% or 97.2% etc.. 当我希望它像99.99%或97.2%等时,它似乎总是四舍五入。

You don't specify what database you are using. 您没有指定要使用的数据库。 However, some databases do integer arithmetic, so 1/2 is 0 not 0.5. 但是,某些数据库执行整数运算,因此1/2是0而不是0.5。

To fix this, just make the constants you are using numeric rather than integer: 要解决此问题,只需使您使用数字而不是整数的常量成为可能:

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

It will then convert to a non-integer type for the arithmetic. 然后,它将转换为非整数类型进行算术运算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM