Mysql query to select a distinct column and the count of a value in another column where column like distinct coumn?

后端 未结 1 551
慢半拍i
慢半拍i 2021-01-29 11:26

My data table is as below:

ID WEEK   RESULT 
1   13     GOOD
2   13     BAD
3   13     GOOD
4   14     GOOD
5   14     BAD
6   15     BAD

I nee

相关标签:
1条回答
  • You can use an aggregate function with a CASE expression to convert the rows into columns:

    select week,
      sum(case when result = 'good' then 1 else 0 end) GoodResult,
      sum(case when result = 'bad' then 1 else 0 end) BadResult
    from yt
    group by week;
    

    See SQL Fiddle with Demo

    0 讨论(0)
提交回复
热议问题