在网上看到个SQL题目 不会做,来请教下各位.这个题怎么做啊教师号  星期号 是否有课
 1    2   有
 1    3   有
 2    1   有
 3    2   有`
 1    2   有
写一条sql语句让你变为这样的表
教师号 星期一 星期二 星期三
 1       2   1 
 2   1   
 3       1
各星期下的数字表示:对应的教师在星期几已经排的课数

解决方案 »

  1.   

    这个是行列转换问题,用case when ... else 就可以实现
      

  2.   

    一般都是这样的答案
    select teacherno as 教师号
    ,sum(case when weekno = 1 and hadcourse = '有' then 1 else 0 end) as 星期一
    ,sum(case when weekno = 2 and hadcourse = '有' then 1 else 0 end) as 星期二
    ,sum(case when weekno = 3 and hadcourse = '有' then 1 else 0 end) as 星期三
    ,sum(case when weekno = 4 and hadcourse = '有' then 1 else 0 end) as 星期四
    ,sum(case when weekno = 5 and hadcourse = '有' then 1 else 0 end) as 星期五
     from teacher group by teachernoORACLE里面 decode 函数应该更简单。