+------------+---------+------------+--------------+------+
| 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
明智的失败百分比,但我尝试的却是100%失败( baaaaad编码器)。
+------------+---------+------------+--------------+------+
| 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;
答案 0 :(得分:1)
您可以通过以下代码获得失败或成功组的百分比。
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
答案 1 :(得分:1)
主要问题似乎是要正确理解GROUP BY。它只表示每个______"显示一个结果行。您希望每个code
有一个结果行,因此您group by code
。如果您想要每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;