从多列值计算百分比
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从多列值计算百分比相关的知识,希望对你有一定的参考价值。
我想找到mysql查询来计算基于不同列上的值完成的工作百分比,以前我在Microsoft Excel中使用countif做到了这一点,但现在我想在mysql上创建相同的表。
这是我当前的表格
Parts Already Build :
------------------------------------------------------------------
| id | Front | Rear | Left | Right | Up | Down |
__________________________________________________________________
| 01 | Done | Done | Done | Done | Done | Done |
------------------------------------------------------------------
| 02 | Done | Done | Done | Done | No Need | No Need |
------------------------------------------------------------------
| 03 | Done | Ongoing | Ongoing | Done | Done | Not Yet |
------------------------------------------------------------------
| 04 | Not Yet | Not Yet | Not Yet | Not Yet | Not Yet | Not Yet |
------------------------------------------------------------------
预期结果
Parts Already Build :
--------------------------------------------------------------------------------
| id | Front | Rear | Left | Right | Up | Down | % | Status |
________________________________________________________________________________
| 01 | Done | Done | Done | Done | Done | Done | 100 | Complete |
--------------------------------------------------------------------------------
| 02 | Done | Done | Done | Done | No Need | No Need | 100 | Complete |
--------------------------------------------------------------------------------
| 03 | Done | Ongoing | Ongoing | Done | Done | Not Yet | 50 | Ongoing |
--------------------------------------------------------------------------------
| 04 | Not Yet | Not Yet | Not Yet | Not Yet | Not Yet | Not Yet | 0 | Not Yet |
--------------------------------------------------------------------------------
注意:
- 如果完成所有过程,或者在那6列中有6个“完成”值,则%列= 100
- 如果值为“ No Need”,则将其计为“ Done”。
- 如果不是所有过程都完成/没有过程完成,则表将仅计算有多少“完成”或“不需要”值,并将其转换为%列中的百分比(3/6进程为“完成” = 50 %)
- 如果%= 100%,则“状态”列=完成/如果= 0%=尚未/如果其他(1%-99%=正在进行)
当前查询:
SELECT
front,
rear,
left,
right,
up,
down,
_________________ AS %,
_________________ AS Status
FROM process
答案
SELECT `Front`, `Rear`, `Left`, `Right`, `Up`, `Down`,
complete * 100 / 6 AS percent,
CASE complete WHEN 6 THEN 'Complete'
WHEN 0 THEN 'Not Yet'
ELSE 'Ongoing'
END AS status
FROM (SELECT *,
0 + (Front = 'Done' OR Front = 'No Need')
+ (Rear = 'Done' OR Rear = 'No Need')
+ (`Left` = 'Done' OR `Left` = 'No Need')
+ (`Right` = 'Done' OR `Right` = 'No Need')
+ (Up = 'Done' OR Up = 'No Need')
+ (Down = 'Done' OR Down = 'No Need') AS complete
FROM process) p
另一答案
two另一答案
None以上是关于从多列值计算百分比的主要内容,如果未能解决你的问题,请参考以下文章