Oracle

--创建表,插入资料
create table lx
( sno number(10),
course varchar2(10),
score number(10)
)
insert into lx
select 3,'语文',90 from dual
union
select 3,'数学',68 from dual
select * from lx

写法1:
将语文成绩,数学成绩 分别做一个表,然后两者用学号关联,再比较成绩;
select a.sno, a.score 语文 ,b.score 数学
from
(select * from lx where course='语文')a,
(select * from lx where course='数学')b
where a.sno=b.sno and a.score>b.score
写法2:行转列,列出 学号,语文成绩,数学成绩 ,然后再比较;
行转列:
select sno,
case when course='语文' then score else 0 end yw,
case when course='数学' then score else 0 end sx
from lx

做一个汇总,列成 学号,语文成绩,数学成绩 的格式;
select sno, max(yw) yw, max(sx) sx from
(
select sno,
case when course='语文' then score else 0 end yw,
case when course='数学' then score else 0 end sx
from lx
)b group by sno

然后对分组后的数据做过滤:having
select sno, max(yw) yw, max(sx) sx from
(
select sno,
case when course='语文' then score else 0 end yw,
case when course='数学' then score else 0 end sx
from lx
)b group by sno having max(yw)>max(sx)

或者 max 与 case when 结合:
select * from
(
select sno,
max(case when course='语文' then score else 0 end )yw,
max(case when course='数学' then score else 0 end )sx
from lx group by sno
) b where yw>sx















