表中的数据如下:
SQL> select * from tb;
ID CHINESE MATH ENGLISH SPORTS
---------- ---------- ---------- ---------- ----------
1001 89 98 87 63
1002 81 87 79 97
1003 65 86 65 87
1004 87 82 89 84
1005 76 76 87 79
1006 90 68 67 94
1007 56 65 86 69希望能查询出每个人参加的4个科目中的最大值和最小值,结果如下:
TID CHINESE MATH ENGLISH SPORTS MAXSUB MINSUB
---------- ---------- ---------- ---------- ---------- ---------- ----------
1001 89 98 87 63 98 63
1002 81 87 79 97 97 79
1003 65 86 65 87 87 65
1004 87 82 89 84 89 82
1005 76 76 87 79 87 76
1006 90 68 67 94 94 67
1007 56 65 86 69 86 56我所写的sql语句为:
select
tid,chinese,math,english,sports,maxsub,minsub
from
tb,
(select
tb.id tid,max(cc) maxsub,min(cc) minsub
from(
select id,chinese cc from tb union all
select id,math from tb union all
select id,english from tb union all
select id,sports from tb) t,tb
where
t.id=tb.id group by tb.id
) t2
where
tb.id=tid
order by
tid
但是本人觉得本人所写的sql语句不好,故求各位高手能写一个比较容易理解的SQL语句出来,谢谢!
greatest(CHINESE, MATH,ENGLISH,SPORTS),
least(CHINESE, MATH,ENGLISH,SPORTS)
from tb;
今天见识了,原来用oracle可以写的更简单!感谢楼上!
Chinese,
Math,
English,
Sports,
Greatest(Chinese, Math, English, Sports),
Least(Chinese, Math, English, Sports)
From Tb;