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