sql group by 2字段计数?

时间:2018-12-25 14:02:59

标签: mysql database

name status a win a run a run b run b win b win c run c run c run d run d run e NA e run e win f NA f NA f NA

在mysql

在这里,我需要计算跑步次数,并且赢球和NA也不相同

有人可以帮我吗?

期望的输出 WIN 3 RUN 2 NA 1

尝试过的查询 SELECT a FROM data GROUP BY a HAVING ( COUNT(*) > 1 )

1 个答案:

答案 0 :(得分:0)

尝试使用条件聚合:

SELECT
    name,
    COUNT(CASE WHEN status = 'win' THEN 1 END) AS WIN,
    COUNT(CASE WHEN status = 'run' THEN 1 END) AS run,
    COUNT(CASE WHEN status = 'NA'  THEN 1 END) AS NA
FROM yourTable
GROUP BY
    name;