简体   繁体   English

如何计算 SQL 中的胜率?

[英]How to calc Win Rate in SQL?

I am trying to calculate win rate for the below table in SQL but not getting a correct answer我正在尝试计算 SQL 中下表的胜率,但没有得到正确答案

在此处输入图像描述

What I am looking for is WON = 1111/1496*100我正在寻找的是 WON = 1111/1496*100

So far i've got到目前为止我有

SELECT Sum( Status = 'Won') /(Select Count(Status))*100 as Win_rate
FROM table

If i run the above it gives me 0 .如果我运行上面它给我0

Your problem is integer division.你的问题是 integer 划分。 Both operands of the division are integers, so SQLite forces an integer result.除法的两个操作数都是整数,因此 SQLite 强制产生 integer 结果。 A typical workaround is force decimal context, like:一个典型的解决方法是强制十进制上下文,例如:

SELECT 100.0 * sum(Status = 'Won') / Count(*) as Win_rate FROM mytable

But it is simpler to use avg() here:但是在这里使用avg()更简单:

SELECT avg(Status = 'Won') * 100 as Win_rate
FROM mytable

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

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