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 5250 게시물 읽기
No. 5250
고수님들 도와주세횸.. 굽신 굽신..
작성자
쿼리어려버
작성일
2009-07-13 14:19
조회수
5,033

출력할 데이터가 하나 있습니다.

화면상에서 각 필드의 내용마다 테이블의 하나의 레코드로 입력을 합니다.

이 내용을 쿼리를 통해서. 결과를 가져 오려고 합니다.

tempAppNo mainQuestionNo subQuestionNo insuredType answer
20090713001737 1 0 0 Y
20090713001737 1 1 0 Y
20090713001737 1 2 0 Y
20090713001737 2 0 0 Y
20090713001737 2 0 1 Y
20090713001737 2 1 0 C
20090713001737 2 1 1 Y
20090713001737 4 0 0 Y
20090713001737 5 0 0 N
20090713001737 6 0 0 N
20090713001737 7 0 0 N


이 테이블의 내용을 가지고 answer 부분만 발췌 하여
   
mainQuestionNo  quest01(answer)  quest02(answer) ....... quest08(answer)
                  1                      Y                      Y                         N(데이터가 없으므로)
                  2                      Y                      Y                         N
                  4                      Y                      N                         N
                  5                      N                      N                        N

이런 형태로 가져오고 싶습니다.
물론 answer 값은 Y/N 값이 되기도 하고 일반 텍스트 문장이 될수 있습니다.

고수님들 도움 부탁 드립니다. 굽신 굽신..

키값은
tempAppNo mainQuestionNo subQuestionNo insuredType

필드의 내용입니다... 도와주세효.

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

문제 이해가 잘가지않네여

mainQuestionNo 에 subQuestionNo 각 고유로 존재해야 되는게 아닌지여?

제가 생각하는게 맞다면 


select tempAppNo, mainQuestionNO,
case when answer1 is null then 'N' else answer1 end answer01,
case when answer2 is null then 'N' else answer2 end answer02,
case when answer3 is null then 'N' else answer3 end answer03,
case when answer4 is null then 'N' else answer4 end answer04,
case when answer5 is null then 'N' else answer5 end answer05,
case when answer6 is null then 'N' else answer6 end answer06,
case when answer7 is null then 'N' else answer7 end answer07,
case when answer8 is null then 'N' else answer8 end answer08
from
(
 select tempAppNo, mainQuestionNO,
 max(answerA1) as answer1,
 max(answerA2) as answer2,
 max(answerA3) as answer3,
 max(answerA4) as answer4,
 max(answerA5) as answer5,
 max(answerA6) as answer6,
 max(answerA7) as answer7,
 max(answerA8) as answer8
 from
  (
  select
  tempAppNo, mainQuestionNO,
  case when subQuestionNO = 0 then answer end as answerA1,
  case when subQuestionNO = 1 then answer end as answerA2,
  case when subQuestionNO = 2 then answer end as answerA3,
  case when subQuestionNO = 3 then answer end as answerA4,
  case when subQuestionNO = 4 then answer end as answerA5,
  case when subQuestionNO = 5 then answer end as answerA6,
  case when subQuestionNO = 6 then answer end as answerA7,
  case when subQuestionNO = 7 then answer end as answerA8
  from btest
  ) t1
 group by tempAppNo, mainQuestionNO
) t2

임진표(운가라)님이 2009-07-14 10:08에 작성한 댓글입니다.
select a.mainQuestionNo,
      isnull(max(a.quest00),'N') as quest00,
      isnull(max(a.quest01),'N') as quest01,
      isnull(max(a.quest02),'N') as quest02,
      isnull(max(a.quest03),'N') as quest03,
      isnull(max(a.quest04),'N') as quest04,
      isnull(max(a.quest05),'N') as quest05,
      isnull(max(a.quest06),'N') as quest06
from (
      select mainQuestionNo,
            case subQuestionNo when '0' then answer end as quest00,
            case subQuestionNo when '1' then answer end as quest01,
            case subQuestionNo when '2' then answer end as quest02,
            case subQuestionNo when '3' then answer end as quest03,
            case subQuestionNo when '4' then answer end as quest04,
            case subQuestionNo when '5' then answer end as quest05,
            case subQuestionNo when '6' then answer end as quest06
       from tttttt
      ) a
group by a.mainQuestionNo

결과는 
1 Y Y Y N N N N
2 Y Y N N N N N
4 Y N N N N N N
5 N N N N N N N
6 N N N N N N N
7 N N N N N N N


중복되는건 잘 처리하면 될꺼같습니다.
유성만(tiptop)님이 2009-08-27 17:17에 작성한 댓글입니다.
이 댓글은 2009-08-27 17:22에 마지막으로 수정되었습니다.
[Top]
No.
제목
작성자
작성일
조회
5260스키마명을 명시해줘야 하는 문제
김종화
2009-07-16
5622
5252OLE DB 문제입니다 ㅠㅠ
enteris
2009-07-14
5440
5251고민고민끝에 질문드립니다. SQL query [2]
이가루
2009-07-13
5398
5250고수님들 도와주세횸.. 굽신 굽신.. [2]
쿼리어려버
2009-07-13
5033
5219갯수 구하려고 하는데 어디를 수정해야 할 지 모르겠어요..;;
박수영
2009-07-09
5375
5209날짜 별로 카우터 가져올때 없는 날짜는 0으로 표시 [1]
쏠라-C
2009-07-08
5802
5181여러 테이블 JOIN 해서 결과 내는 방법 문의 [1]
조현철
2009-07-06
5648
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.017초, 이곳 서비스는
	PostgreSQL v16.2로 자료를 관리합니다