[英]Getting failure percentage for a column
+------------+---------+------------+--------------+------+
| dt | status | code | arena | cnt |
+------------+---------+------------+--------------+------+
| 2016-01-01 | failure | AB | kingdom1 | 2 |
| 2016-01-01 | success | AB | kingdom1 | 16 |
| 2016-01-01 | failure | CD | kingdom1 | 50 |
| 2016-01-01 | success | CD | kingdom1 | 662 |
| 2016-01-01 | failure | EF | kingdom1 | 131 |
| 2016-01-01 | success | EF | kingdom1 | 622 |
SQL查詢:
select DATE(created) as dt, status, code, arena,count(status) as cnt
from game_table
where some_condition
group by dt, code, status)
要求:
我需要找到code
失敗百分比,但在arena
還是不錯 ,但是我嘗試的是為所有code
返回100%失敗( baaaaad coder )。
+------------+---------+------------+--------------+------+
| dt | status | code | arena |failed|
+------------+---------+------------+--------------+------+
| 2016-01-01 | failure | AB | kingdom1 | 11.1 |
| 2016-01-01 | failure | CD | kingdom1 | 7.02 |
| 2016-01-01 | failure | EF | kingdom1 | 17.4 |
我試過了 :
select dt, cnt/sum(cnt) as p, status, code, arena
from (
select DATE(created) as dt, status, code, arena,count(status) as cnt
from game_table
where some_condition
group by dt, code, status
) as inner_t
group by dt, code, status;
您可以通過以下代碼獲取失敗或成功組的百分比。
select DATE(created) as dt, count(case when status = "failure" then 1 end) * 100/count(status) as failure, code, arena, cnt
from percent_issue group by code
主要問題似乎是正確理解GROUP BY。 它僅表示“每______向我顯示一個結果行”。 您似乎希望每個code
一個結果行,所以您group by code
。 如果您希望每個code
和arena
一行,則按code
arena
group by code, arena
。 對於不在GROUP BY
的字段,請確定要顯示的匯總,例如最短日期。
select
min(created)
'failure' as status,
code,
min(arena) as arena,
count(case when status = 'failure' then 1 end) / count(*) * 100 as failed
from game_table
where some_condition
group by code;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.