database.sarang.net
UserID
Passwd
Database
DBMS
MySQL
PostgreSQL
Firebird
Oracle
Informix
Sybase
ㆍMS-SQL
DB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
MS-SQL Q&A 4277 게시물 읽기
No. 4277
가장 큰 값으로 정렬하기
작성자
소병민(qudals)
작성일
2008-06-13 20:30ⓒ
2008-06-13 20:31ⓜ
조회수
5,722

이름 성적1 성적2 성적3 성적4 성적5
AAA 52 78 34 0 43
BBB 46 54 75 65 24
CCC 78 86 45 56 12
DDD 53 77 13 44 67
EEE 34 24 63 23 83


위와 같은 데이터가 있을 경우에

각 이름들의 최고 성적만을 모아서 순위를 정해서 아래와 같은 결과가 필요합니다.


순위 이름 최고성적
1 CCC 86
2 EEE 83
3 AAA 78
4 DDD 77
5 BBB 75


쿼리로 한방에 날릴 수는 없을까요?

여러 필드중에서 가장 큰 값을 찾는 것이 좀 어렵네요.

이 글에 대한 댓글이 총 2건 있습니다.

 

create table test(이름 varchar(3),성적1 int,성적2 int,성적3 int,성적4 int,성적5 int)

insert test

select 'AAA',52,78,34,0,43 union all

select 'BBB',46,54,75,65,24 union all

select 'CCC',78,86,45,56,12 union all

select 'DDD',53,77,13,44,67 union all

select 'EEE',34,24,63,23,83

 

------------------------------------------

--MSSQL 2000

 

select identity(int,1,1) 순위,

       이름,

       max(case b.id when 1 then 성적1

                     when 2 then 성적2

                     when 3 then 성적3

                     when 4 then 성적4

                     when 5 then 성적5 end) 성적 into #tmp    

 from test a cross join

      (select 1 id union all

       select 2 union all

       select 3 union all

       select 4 union all

       select 5) b

 group by 이름

 order by 성적 desc

 

select * from #tmp

 

/*

순위          이름   성적

----------- ---- -----------

1           CCC  86

2           EEE  83

3           AAA  78

4           DDD  77

5           BBB  75

 

(5 적용됨)

*/

 

 

--MSSQL 2005

select row_number() over(order by 성적 desc) 순위,*

  from (select 이름,

               max(case b.id when 1 then 성적1

                             when 2 then 성적2

                             when 3 then 성적3

                             when 4 then 성적4

                             when 5 then 성적5 end) 성적        

              from test a cross join

                  (select 1 id union all

                   select 2 union all

                   select 3 union all

                   select 4 union all

                   select 5) b

            group by 이름) t

 order by 성적 desc

 

/*

이름   성적

---- -----------

AAA  78

BBB  75

CCC  86

DDD  77

EEE  83

 

(5 적용됨)

 

*/

최석준(beatchoi)님이 2008-06-16 09:51에 작성한 댓글입니다.

create table rank_test
 (
  name varchar(3),
  score1 int,
  score2 int,
  score3 int,
  score4 int,
  score5 int
 )

insert into rank_test values('AAA',52,78,34,0,43 )
insert into rank_test values('BBB',46,54,75,65,24 )
insert into rank_test values('CCC',78,86,45,56,12 )
insert into rank_test values('DDD',53,77,13,44,67 )
insert into rank_test values('EEE',34,24,63,23,83 )

select rank() over(order by max(t1.score1) desc) rank , name, max(t1.score1) max_score
from
 (
  select score1,name from rank_test
  union all
  select score2,name from rank_test
  union all
  select score3,name from rank_test
  union all
  select score4,name from rank_test
  union all
  select score5,name from rank_test
 )t1
group by t1.name
지나가다님이 2008-06-16 10:46에 작성한 댓글입니다.
이 댓글은 2008-06-16 10:48에 마지막으로 수정되었습니다. Edit
[Top]
No.
제목
작성자
작성일
조회
4280MSSQL 2005 서버 클러스터링 ~~ [3]
김정운
2008-06-16
6860
4279인터페이스 설정에 대해 여쭙니다.
용가리
2008-06-16
4382
4278[급]쿼리 크로스탭 및 정렬 관련 문의 [1]
이광훈
2008-06-15
5788
4277가장 큰 값으로 정렬하기 [2]
소병민
2008-06-13
5722
4276오라클 Linked Server 이용 insert시 메모리 부족 오류
박종필
2008-06-13
6333
4275날짜 마스터테이블 데이터 생성 [1]
네네
2008-06-13
6570
4274연비계산하는 로직을 생각하고 있습니다. [1]
연비
2008-06-13
4680
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2023 DSN, All rights reserved.
작업시간: 0.052초, 이곳 서비스는
	PostgreSQL v16.1로 자료를 관리합니다