declare @t table(name1 varchar(10), subject1 varchar(10), subject2 varchar(10), score1 int, score2 int)
insert @t
select '张三',   '数学',      '语文',       80,    60 union all
select '李四',   '数学',      '语文',       90,    40 union all
select '王五',   '数学',      '语文',       50,    70select 科目,姓名 from 
(select 科目='数学',姓名='',编号=1 union all
select '语文','',2 union all
select '',name1,1 from @T where score1>=60 union all
select '',name1,2 from @t where score2>=60)t
order by t.编号,t.姓名/*结果
数学
      张三
      李四
语文
      张三
      王五*/

解决方案 »

  1.   

    如果科目不定是什么科目,可以这样:select 科目,姓名 from 
    (select top 1 科目=subject1,姓名='',编号=1 from @t 
    union all
    select top 1 科目=subject2,'',2 from @t 
    union all
    select '',name1,1 from @T where score1>=60 
    union all
    select '',name1,2 from @t where score2>=60)t
    order by t.编号,t.姓名
      

  2.   

    刚才真的已经脑筋停顿了。原来只要用两个select分别统计不同科目的人数,然后再union all 就行了。