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 3675 게시물 읽기
No. 3675
월별 순위리스트작성에서 동률에 대한 문제..
작성자
유성만(tiptop)
작성일
2007-08-08 11:31
조회수
3,048

1월과 2월에대한 쿼리만 보여드리겠습니다..  (mo는 당월값 mc는 누적값)

select rank,

     isnull(max(case when month = '01' and c.this_is_cumulate = 'MO' then c.performance_department_name end), '') as name_mo1,

     isnull(sum(case when month = '01' and c.this_is_cumulate = 'MO' then score_value end),0) as score_mo1,

     isnull(max(case when month = '01' and c.this_is_cumulate = 'MC' then c.performance_department_name end), '') as name_mc1,

     isnull(sum(case when month = '01' and c.this_is_cumulate = 'MC' then score_value end),0) as score_mc1,

     isnull(max(case when month = '02' and c.this_is_cumulate = 'MO' then c.performance_department_name end), '') as name_mo2,

     isnull(sum(case when month = '02' and c.this_is_cumulate = 'MO' then score_value end),0) as score_mo2,

     isnull(max(case when month = '02' and c.this_is_cumulate = 'MC' then c.performance_department_name end), '') as name_mc2,

     isnull(sum(case when month = '02' and c.this_is_cumulate = 'MC' then score_value end),0) as score_mc2

from

     (

     select a.rank,

          a.month,

          a.this_is_cumulate,

          a.performance_department_code,

          b.performance_department_name,

          a.score_value

     from  hbi_performance_department_rank a

     left join hbi_performance_department b

     on    a.performance_department_code = b.performance_department_code

     and   a.year = b.year

     where a.year = '2007'

     and   b.upper_performance_department_code = '100000000'

     ) as c

group by rank

order by rank 


위의 쿼리에 의해 구해진 값은 아래와같습니다.

rank name_mo1 score_mo1 name_mc1 score_mc1 name_mo2 score_mo2 name_mc2 score_mc2
1 플랜트사업본부 94 현장 162 건축사업본부 72 건축사업본부 96
2 토목사업본부 83   0 주택사업본부 49 플랜트사업본부 82
3 기술지원실 78 기술지원실 69 플랜트사업본부 46 토목사업본부 79
4 기획개발실 65 토목사업본부 61 토목사업본부 42 주택사업본부 106
5 해외사업본부 59 플랜트사업본부 54 해외사업본부 80   0
6 주택사업본부 56 기획개발실 46   0 현장 41
7 건축사업본부 39 해외사업본부 45 경영지원실1 33 기획조정실 34
8 현장 31 기획조정실 14 기획개발실 15 기술지원실 23
9 경영지원실1 28 경영지원실1 13 기획조정실 8 해외사업본부 7
10 기획조정실 21 주택사업본부 4 현장 5 경영지원실1 3


1월 mo에대한값은 정상입니다..  나머지  1mc 2mo 2mc에 대해서는 

아래쿼리로 확인해봤습니다.


select a.performance_department_code,

       b.performance_department_name,

       a.this_is_cumulate,

       a.rank,

       a.score_value

from  hbi_performance_department_rank a

left join hbi_performance_department b

on    a.performance_department_code = b.performance_department_code

and   a.year = b.year

where a.year = '2007'

and   a.month= '01'

and   b.upper_performance_department_code = '100000000'

and   this_is_cumulate = 'mc'

order by rank


performCode.. name cum rank score
100200000 건축사업본부 MC 1 81
101000000 현장 MC 1 81
100900000 기술지원실 MC 3 69
100300000 토목사업본부 MC 4 61
100400000 플랜트사업본부 MC 5 54
100800000 기획개발실 MC 6 46
100500000 해외사업본부 MC 7 45
100600000 기획조정실 MC 8 14
100700000 경영지원실1 MC 9 13
100100000 주택사업본부 MC 10 4



2월 mo 결과

performCode.. name cum rank score
100200000 건축사업본부 MO 1 72
100100000 주택사업본부 MO 2 49
100400000 플랜트사업본부 MO 3 46
100300000 토목사업본부 MO 4 42
100500000 해외사업본부 MO 5 40
100900000 기술지원실 MO 5 40
100700000 경영지원실1 MO 7 33
100800000 기획개발실 MO 8 15
100600000 기획조정실 MO 9 8
101000000 현장 MO 10 5


랭크가 동률일때 저런 현상이 발생합니다

sum을 max함수로 바꾸면 값은 제대로 나오지만 한 항목이 누락되는것은 마찬가지입니다..

이문제를 어떻게 해결해야할까요?

2일동안 고민중인데...  제머리에서는 더이상 나올게 없네요..  좋은 의견 부탁드리겠습니다.

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

대문자가 추가된부분임.
SUM을 MAX로 바꿔도 될듯함.

select rank, SEQ
     isnull(max(case when month = '01' and c.this_is_cumulate = 'MO' then c.performance_department_name end), '') as name_mo1,
     isnull(sum(case when month = '01' and c.this_is_cumulate = 'MO' then score_value end),0) as score_mo1,
     isnull(max(case when month = '01' and c.this_is_cumulate = 'MC' then c.performance_department_name end), '') as name_mc1,
     isnull(sum(case when month = '01' and c.this_is_cumulate = 'MC' then score_value end),0) as score_mc1,
     isnull(max(case when month = '02' and c.this_is_cumulate = 'MO' then c.performance_department_name end), '') as name_mo2,
     isnull(sum(case when month = '02' and c.this_is_cumulate = 'MO' then score_value end),0) as score_mo2,
     isnull(max(case when month = '02' and c.this_is_cumulate = 'MC' then c.performance_department_name end), '') as name_mc2,
     isnull(sum(case when month = '02' and c.this_is_cumulate = 'MC' then score_value end),0) as score_mc2
from
     (
     select a.rank,
          (SELECT COUNT(*)
             FROM hbi_performance_department_rank B
            WHERE B.month             = a.month
              AND B.rank              = a.rank
              AND B.this_is_cumulate  = a.this_is_cumulate
              AND B.performance_department_code
                                     <= a.performance_department_code) SEQ,
          a.month,
          a.this_is_cumulate,
          a.performance_department_code,
          b.performance_department_name,
          a.score_value
     from  hbi_performance_department_rank a
     left join hbi_performance_department b
     on    a.performance_department_code = b.performance_department_code
     and   a.year = b.year
     where a.year = '2007'
     and   b.upper_performance_department_code = '100000000'
     ) as c
group by rank , SEQ

예상결과

1 1 플랜트사업본부 94 건축사업본부 81 건축사업본부   72 건축사업본부   96 ...
1 2                 0 현장         81                 0                 0
2 1 토목사업본부   83              0 주택사업본부   49 플랜트사업본부 82
3 1 기술지원실     78 기술지원실   69 플랜트사업본부 46 토목사업본부   79
4 1 기획개발실     65 토목사업본부 61 토목사업본부   42 주택사업본부   53
4 2                 0               0                 0 기획개발실     53
.
.
.

박진복(pjb708)님이 2007-08-09 09:27에 작성한 댓글입니다.
이 댓글은 2007-08-09 09:47에 마지막으로 수정되었습니다.

윗쿼리에대한 결과..

1 1   0   0   0   0
2 1   0   0 주택사업본부 49   0
4 1   0   0   0 주택사업본부 53
5 1   0   0   0   0
6 1 주택사업본부 56   0   0   0
9 1 경영지원실1 28 경영지원실1 13 기획조정실 8 해외사업본부 7
10 1 기획조정실 21 주택사업본부 4 현장 5 경영지원실1 3
1 2   0 건축사업본부 81 건축사업본부 72 건축사업본부 96
2 2   0   0   0   0
4 2   0 토목사업본부 61 토목사업본부 42   0
5 2 해외사업본부 59 플랜트사업본부 54 해외사업본부 40   0
6 2   0 기획개발실 46   0 현장 41
7 2 건축사업본부 39 해외사업본부 45 경영지원실1 33 기획조정실 34
8 2 현장 31 기획조정실 14 기획개발실 15 기술지원실 23
9 2   0   0   0   0
1 3   0   0   0   0
2 3 토목사업본부 83   0   0   0
3 3   0   0   0 토목사업본부 79
4 3   0   0   0   0
5 3   0   0 기술지원실 40   0
7 3   0   0   0   0
1 4 플랜트사업본부 94   0   0   0
2 4   0   0   0 플랜트사업본부 82
3 4   0   0 플랜트사업본부 46   0
4 4 기획개발실 65   0   0   0
1 5   0   0   0   0
2 5   0   0   0   0
3 5   0   0   0   0
4 5   0   0   0 기획개발실 53
1 6   0   0   0   0
3 6   0   0   0   0
1 7   0   0   0   0
2 7   0   0   0   0
3 7 기술지원실 78 기술지원실 69   0   0
1 8   0   0   0   0
2 8   0   0   0   0
1 9   0   0   0   0
1 11   0 현장 81   0   0

유성만(tiptop)님이 2007-08-09 14:29에 작성한 댓글입니다.

답변감사합니다..
이런저런태이블구조다 다 안보여드렸는데 생각해주셔서 감사합니다..

이쿼리를 제가 만든게 아니라 지난번 프로젝트 쿼리인데 제가 사용하려고 분석해보니까 랭킹 동률일때 문제가 발생해서...  지난번프로젝트도 문제가 되버렸네요...
최초 쿼리 생성자가 다시 고민을 해본다고 합니다..
좋은 로직이 완성되면 댓글에 올리겠습니다..
수고하세요~

유성만(tiptop)님이 2007-08-09 14:34에 작성한 댓글입니다.

DB2 사용자라 테스트를 못했습니다.
order by rank , SEQ 를 추가해보세요.

참고로 DB2 SAMPLE입니다.

WITH
 TBL(MNT,COD,RNK,SCO) AS (VALUES
 (1,'111',1,90)
,(1,'222',2,80)
,(1,'333',2,80)
,(1,'444',5,10)
,(1,'555',4,20)
,(2,'111',3,60)
,(2,'222',5,40)
,(2,'333',3,60)
,(2,'444',2,70)
,(2,'555',1,90)
,(3,'111',1,95)
,(3,'222',5,45)
,(3,'333',1,95)
,(3,'444',4,65)
,(3,'555',3,75)
)

SELECT  RNK ,SEQ
       ,VALUE(MAX(CASE WHEN MNT=1 THEN COD ELSE ' ' END),' ')
       ,VALUE(MAX(CASE WHEN MNT=1 THEN SCO ELSE 0 END),0)
       ,VALUE(MAX(CASE WHEN MNT=2 THEN COD ELSE ' ' END),' ')
       ,VALUE(MAX(CASE WHEN MNT=2 THEN SCO ELSE 0 END),0)
       ,VALUE(MAX(CASE WHEN MNT=3 THEN COD ELSE ' ' END),' ')
       ,VALUE(MAX(CASE WHEN MNT=3 THEN SCO ELSE 0 END),0)
  FROM (
    SELECT  MNT,COD,RNK,SCO
           ,(SELECT COUNT(*)
               FROM TBL T2
              WHERE T2.MNT  = T1.MNT
                AND T2.RNK  = T1.RNK
                AND T2.COD <= T1.COD) SEQ
      FROM  TBL T1) AA
GROUP BY RNK ,SEQ


결과:

1   1   111 90  555 90  111 95
1   2        0       0  333 95
2   1   222 80  444 70       0
2   2   333 80       0       0
3   1        0  111 60  555 75
3   2        0  333 60       0
4   1   555 20       0  444 65
5   1   444 10  222 40  222 45

박진복(pjb708)님이 2007-08-09 15:02에 작성한 댓글입니다.
이 댓글은 2007-08-09 15:05에 마지막으로 수정되었습니다.
[Top]
No.
제목
작성자
작성일
조회
3680로그인 로그아웃 시간 평균 내는 쿼리좀 알려 주세요 [1]
급질문
2007-08-09
2621
3679sql 질의식 질문입니다 [1]
노주원
2007-08-09
2180
3677ms-sql2000에서 ms-sql2005로접속 [1]
훈00
2007-08-08
2495
3675월별 순위리스트작성에서 동률에 대한 문제.. [4]
유성만
2007-08-08
3048
3673셀렉트 질문입니다. [4]
Wenzie
2007-08-06
2662
3672자료 변환에 대한 문의 드립니다. [1]
자료형
2007-08-04
2393
3671timestamp 에 관한 의문점 [1]
정영철
2007-08-03
2511
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.017초, 이곳 서비스는
	PostgreSQL v16.2로 자료를 관리합니다