本帖最后由 laozishi110 于 2012-05-09 09:53:55 编辑

解决方案 »

  1.   


    select studentname,
    sum(case when course = '语文' then score else 0 end) as '语文' ,
    sum(case when course = '英语' then score else 0 end) as '英语' 
    from TB 
    group by studentname
    /*
    studentname 语文          英语
    ----------- ----------- -----------
    李三          50          80
    王五          60          70(2 行受影响)
      

  2.   

    select TF.studentname,TF.course,TF.score,TT.course,TT.score
    from 
    (select *
    from TB
    where course='语文') TF
    inner join (select * from TB where course='英语') TT on TT.studentname =TF.studentname
    /*
    studentname course score       course score
    ----------- ------ ----------- ------ -----------
    李三          语文     50          英语     80
    王五          语文     60          英语     70(2 行受影响)
      

  3.   

    行专列 SQL 2000用2楼
    SQL 2005以上select studentname,语文,英语
    from CourseGrade
    pivot (max(score) for course in([语文],英语)) as d studentname 语文 英语
    李三 50 80
    王五 60 70并列显示
    select A.studentname,A.course,A.score,B.course,B.score
    from CourseGrade AS A INNER JOIN CourseGrade AS B ON A.studentname = B.studentname AND A.course = '语文' AND B.course = '英语'studentname course score course score
    李三 语文 50 英语 80
    王五 语文 60 英语 70