Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

MySQL GROUP BY uma coluna e mostra valores em colunas diferentes

select id2, time,
       sum(case when id3 = 0 then val else 0 end) as col0,
       sum(case when id3 = 1 then val else 0 end) as col1,
       sum(case when id3 = 2 then val else 0 end) as col2,
       sum(case when id3 = 3 then val else 0 end) as col3,
       sum(case when id3 = 4 then val else 0 end) as col4
from your_table
group by id2, time
having sum(val) > 0