sql按2字段计数分组?

sd2nnvve  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(331)

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中
在这里我需要采取的运行计数也不赢一样的赢和不赢
有人能帮我吗?
预期产量 WIN 3 RUN 2 NA 1 已尝试的查询 SELECT a FROM data GROUP BY a HAVING ( COUNT(*) > 1 )

7vux5j2d

7vux5j2d1#

尝试使用条件聚合:

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;

相关问题