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
운영게시판
최근게시물
PostgreSQL Q&A 10204 게시물 읽기
No. 10204
postgresql 테이블 조인 결과 쿼리를 만드려고 합니다.(가로나열)
작성자
디비디비딥(yahoo2344)
작성일
2020-10-28 20:30
조회수
1,428

room 테이블

no     roomName   teacher

1          보통반         이름1

2          심화반         이름2

 

score 테이블

room_no    student_no    kor_scoer    eng_score   math_score

     1                   1                 70                  50               90

     1                   2                 90                  80               70

     1                   3                 75                  50               56

                                                  .

                                                  .

                                                  .

 

인 테이블이 있다고 가정하겠습니다. room 테이블과 score테이블은 1:n 관계입니다.

제가 두 테이블은 조인해서 쿼리를 아래처럼 뽑고 싶은데 어떻게 시작해야할 지 잘 모르겠습니다..

아시는분 있으면 조언좀 부탁드립니다

 room_no    scoer1     scoer2     scoer3

      1              70            90            75           <-- 학생 세 명의 국어 점수

      1              50            80            50           <-- 학생 세 명의 영어 점수

      1              90            70            56           <-- 학생 세 명의 수학 점수

                                                  .

                                                  .

                                                  .

 

이렇게 세로가 아닌 가로로 한줄로 세 명의 학생 점수를 한 로우로 표현하고 싶습니다. 

 

이 글에 대한 댓글이 총 1건 있습니다.
with score(room_no, student_no, kor_score, eng_score, math_score) as
(
select 1, 1, 70, 50, 90 union all
select 1, 2, 90, 80, 70 union all
select 1, 3, 75, 50, 56
)
select j.room_no,  
       max(case j.student_no when 1 then j.lecture_score end) score1,
       max(case j.student_no when 2 then j.lecture_score end) score2,
       max(case j.student_no when 3 then j.lecture_score end) score3
  from (
select s.room_no, l.lecture_no, s.student_no, 
       case l.lecture_no when 1 then s.kor_score 
                         when 2 then s.eng_score
                                 when 3 then s.math_score end lecture_score 
  from score s 
cross join (select generate_series(1,3) lecture_no) l
       ) j
 group by j.room_no, j.lecture_no       
 order by j.room_no, j.lecture_no 
구헌수(madcat)님이 2020-10-29 19:17에 작성한 댓글입니다.
[Top]
No.
제목
작성자
작성일
조회
10207컬럼 가공 시 NULL 처리 [2]
궁금
2020-11-06
1419
10206Windows10에서 설치 후 실행 시 에러 [1]
전상도
2020-11-04
1421
10205번역 자잘한 오타... [3]
전상도
2020-10-30
1411
10204postgresql 테이블 조인 결과 쿼리를 만드려고 합니다.(가로나열) [1]
디비디비딥
2020-10-28
1428
10203Postgresql 11이상 SP 작업시 pgadmin4 와 HeidiSQL, DBeaver 사용
lucky
2020-10-28
1524
10202pgadmin3 에서 에러 메시지 [3]
전상도
2020-10-28
1502
10201AQueryTool에서 PostgreSQL을 지원하는 기능을 추가하였습니다.
조동건
2020-10-27
1370
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2023 DSN, All rights reserved.
작업시간: 0.051초, 이곳 서비스는
	PostgreSQL v16.1로 자료를 관리합니다