SQL case关键字

来源:互联网 发布:数据连接自动开启 编辑:程序博客网 时间:2024/06/07 18:36
Use testselect t2.col_1, sum(t2.col_a)as COL_A,sum(t2.col_b)as COL_B,sum(t2.col_c)as COL_Cfrom(select t.col_1,case t.name when 'a' then t.col_2 else 0 end as col_a,case t.name when 'b' then t.col_2 else 0 end as col_b,case t.name when 'c' then t.col_2 else 0 end as col_c    from(select col_1,name,avg(col_2)as col_2 from Result group by col_1,name)as t))as t2group by t2.col_1