将一个count()除以另一个

时间:2014-10-03 14:58:34

标签: mysql sql

尝试通过将IsComplete = 1的行数除以行总数来从SQL查询中获取百分比值。我怎样才能做到这一点?

数据库表:

table

4 个答案:

答案 0 :(得分:2)

由于值始终为0或1,您可以将值相加并除以计数,如:

SELECT SUM(IsComplete) / COUNT(*) AS percent_complete FROM centraldb.project;

如果您愿意,则乘以100。

答案 1 :(得分:1)

由于在聚合函数中不使用null,请尝试:

select count(case when isComplete = 1 then isComplete end) / count(*)
  from project

答案 2 :(得分:1)

SELECT sum(case when isComplete =1 then 1 else 0)/
       count(*)*100  as percentComplete
FROM project

答案 3 :(得分:1)

这应该

SELECT ((SELECT COUNT(*) FROM centraldb WHERE IsComplete = 1) / COUNT(*)) AS percentage FROM centraldb;

For example