select grade,(case max(score) when 'a' then 1 else 0 end) as gscore into tab2 from tab1 group by grade