select 姓名 ,
  max(case 课程 when '语文' then 分数 end )  语文,
  max(case 课程 when '数学' then 分数 end) 数学,
  max(case 课程 when '物理' then 分数  end) 物理
from tb
group by 姓名select 姓名,
case when 课程='语文' then  分数  end  语文,
case when 课程='数学' then  分数   end 数学
from tb
/*这两种结果不一样。等解释*/

解决方案 »

  1.   

    第一种 按照姓名分组后 case when 课程='语文' then  分数  end  语文 会包含多个NULL
    用MAX 函数可以排除NULL 这样每组只对应一个列值
      

  2.   

    第一个有max,只取分数里面最大的  第二个不是
    可以理解为select 姓名 ,
      max(case when 课程='语文' then 分数 end )  语文,
      max(case when 课程='语文' then 分数 end) 数学,
      max(case when 课程='语文' then 分数  end) 物理
    from tb
    group by 姓名
      

  3.   

    分组后group by,取最大值。
      

  4.   

    select 姓名,
    case when 课程='语文' then  分数  end  语文,
    case when 课程='数学' then  分数   end 数学
    from tb
    呵呵,这样你可以看得很清楚了呀,
    如果有几个是会分开在每行的,加个MAX就是取最大值呀
      

  5.   

    区别在于第一种情况 取了最大值 然后排序了 group by
      

  6.   

    case when age<2 then '婴儿'
         when age>=2 and age<=11 then '儿童'
         when age>=12 and age<18 then '少年'
         when age>=18 and age<=30 then '青年'
         when age>30 and age<60 then '中年'
         when age>=60 and age<=100 then '老年'
         else '过百岁寿星'
    from tb;
      

  7.   

    (case when age <2 then '婴儿' 
        when age>=2 and age <=11 then '儿童' 
        when age>=12 and age <18 then '少年' 
        when age>=18 and age <=30 then '青年' 
        when age>30 and age <60 then '中年' 
        when age>=60 and age <=100 then '老年' 
        else '过百岁寿星' end) as '人生就那样'
    from tb;