데이터가 다음과 같이 되었다고 할때 ... --> 홍길동 10 20 30 40 50
다음과 같은 결과를 얻고자 합니다... --> 홍길동 50 40 30
(3등 까지만 표시)
SQL>conn scott/tiger
SQL>create table test (
name varchar2(10),
a number,
b numner,
c number,
d number,
e number);
SQL>create temp (
name varchar2(10),
num number);
SQL>insert into test values ('홍길동',10,20,30,40,50);
SQL> select * from test;
NAME A B C D E
---------- ---------- ---------- ---------- ---------- ----------
홍길동 10 20 30 40 50
SQL>insert into temp select name, a from test;
SQL>insert into temp select name, b from test;
SQL>insert into temp select name, c from test;
SQL>insert into temp select name, d from test;
SQL>insert into temp select name, e from test;
SQL> select * from temp;
NAME NUM
---------- ----------
홍길동 10
홍길동 20
홍길동 30
홍길동 40
홍길동 50
SQL>select (select name from temp where rownum = 1),
(select min(num) from temp t1 where 1 > (select count(num) from temp t2 where t2.num > t1.num)) first,
(select min(num) from temp t1 where 2 > (select count(num) from temp t2 where t2.num > t1.num)) second,
(select min(num) from temp t1 where 3 > (select count(num) from temp t2 where t2.num > t1.num)) third
from dual
/
[결과]
(SELECTNAM FIRST SECOND THIRD
---------- ---------- ---------- ----------
홍길동 50 40 30
물론 아래 처럼 rank를 써도 되구요...
SQL> select
2 max(decode(rank,1,num)) "First",
3 max(decode(rank,2,num)) "Second",
4 max(decode(rank,3,num)) "Third"
5 from (select name, num, rank() over (order by num desc) rank from temp);
First Second Third
---------- ---------- ----------
50 40 30
다음과 같은 결과를 얻고자 합니다... --> 홍길동 50 40 30
(3등 까지만 표시)
SQL>conn scott/tiger
SQL>create table test (
name varchar2(10),
a number,
b numner,
c number,
d number,
e number);
SQL>create temp (
name varchar2(10),
num number);
SQL>insert into test values ('홍길동',10,20,30,40,50);
SQL> select * from test;
NAME A B C D E
---------- ---------- ---------- ---------- ---------- ----------
홍길동 10 20 30 40 50
SQL>insert into temp select name, a from test;
SQL>insert into temp select name, b from test;
SQL>insert into temp select name, c from test;
SQL>insert into temp select name, d from test;
SQL>insert into temp select name, e from test;
SQL> select * from temp;
NAME NUM
---------- ----------
홍길동 10
홍길동 20
홍길동 30
홍길동 40
홍길동 50
SQL>select (select name from temp where rownum = 1),
(select min(num) from temp t1 where 1 > (select count(num) from temp t2 where t2.num > t1.num)) first,
(select min(num) from temp t1 where 2 > (select count(num) from temp t2 where t2.num > t1.num)) second,
(select min(num) from temp t1 where 3 > (select count(num) from temp t2 where t2.num > t1.num)) third
from dual
/
[결과]
(SELECTNAM FIRST SECOND THIRD
---------- ---------- ---------- ----------
홍길동 50 40 30
물론 아래 처럼 rank를 써도 되구요...
SQL> select
2 max(decode(rank,1,num)) "First",
3 max(decode(rank,2,num)) "Second",
4 max(decode(rank,3,num)) "Third"
5 from (select name, num, rank() over (order by num desc) rank from temp);
First Second Third
---------- ---------- ----------
50 40 30
기업100%환급/오라클/자바/스프링/안드로이드/닷넷C#/웹퍼블리싱… | 12-27 | 1983 | ||
[채용예정교육]오라클자바개발잘하는신입뽑기2개월과정,교육전취… | 12-11 | 1453 | ||
53 | [평일주간]100%환급6건,안드로이드,자바,C#,스프링3.2,SQL,힌트/… | 03-15 | 1207 | |
52 | [주말주간]C#, ASP.NET마스터 | 01-31 | 1372 | |
51 | [평일,기업100%환급]SQL기초에서 Schema Object까지 | 01-31 | 1199 | |
50 | [평일야간]HTML5, CSS3,Ajax, jQuery마스터과정 | 01-31 | 1063 | |
49 | [평일주간,평일야간,주말]Spring,MyBatis,Hibernate개발자과정 | 01-19 | 1371 | |
48 | [평일주간,평일야간,주말]안드로이드개발자과정 | 01-11 | 1204 | |
47 | [평일야간,주말주간]JAVA,Network&JSP&Spring,MyBatis,Hibernate | 01-03 | 1706 | |
46 | 기업100%환급/오라클/자바/스프링/안드로이드/닷넷C#/웹퍼블리싱… | 12-27 | 1983 | |
45 | [평일야간,주말]자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis… | 12-19 | 1474 | |
44 | 웹퍼블리싱 마스터(HTML5,CSS3,jQUERY,AJAX,JavaScript) | 12-14 | 1436 | |
43 | [채용예정교육]오라클자바개발잘하는신입뽑기2개월과정,교육전취… | 12-11 | 1453 | |
42 | [평일,기업100%환급]자바기초에서 JDBC, Servlet/JSP까지 | 12-09 | 1168 | |
41 | [평일야간]닷넷(C#,Network,ADO.NET,ASP.NET)마스터 | 12-01 | 1375 | |
40 | [기업100%환급]C#4.0,WinForm,ADO.NET프로그래밍(평일주간(단기)… | 12-01 | 1532 | |
39 | [평일야간,주말]SQL기초에서실무까지(SQL기초,PLSQL,힌트,튜닝) | 12-01 | 1031 |
댓글 없음:
댓글 쓰기