2014-07-25 5 views
0
나는 오라클 SQL에서 5 열에서 값의 순서를 찾아 6

SQL - 주문 값의 열에서이

Col_1, Col_2, Col_3, Col_4, Col_5, Col_6 

0.2 , 0.1 , 0.6 , 0.4 , 0.3 , 21543 

또는 컬럼에 그 순서를 입력 할

, 나는 각 5 출력 열이 일할 수있는 row 여기서 값은 열 이름입니다.

Col_1, Col_2, Col_3, Col_4, Col_5, Col_6, Col_7, Col_8, Col_9, Col_10 

0.2 , 0.1 , 0.6 , 0.4 , 0.3 , Col_2, Col_1, Col_5, Col_4, Col_3 

모든 제안 사항에 감사드립니다.

건배

답변

0

아마 가장 쉬운 방법은 데이터, repivot를 피벗 해제하고 정보를 추출하는 것입니다 : 이것은 단지 하나의 행에서 논리를하는 것보다 효율성이 떨어집니다

select id, col_1, col_2, col_3, col_4, col_5, 
     max(case when seqnum = 1 then which end) as o1, 
     max(case when seqnum = 2 then which end) as o2, 
     max(case when seqnum = 3 then which end) as o3, 
     max(case when seqnum = 4 then which end) as o4, 
     max(case when seqnum = 5 then which end) as o5 
from (select id, col_1, col_2, col_3, col_4, col_5, 
      row_number() over (partition by id, col_1, col_2, col_3, col_4, col_5 order by val) as seqnum 
     from ((select id, col_1, col_2, col_3, col_4, col_5, 'col_1' as which, col_1 as val from table t) union all 
      (select id, col_1, col_2, col_3, col_4, col_5, 'col_2' as which, col_2 as val from table t) union all 
      (select id, col_1, col_2, col_3, col_4, col_5, 'col_3' as which, col_3 as val from table t) union all 
      (select id, col_1, col_2, col_3, col_4, col_5, 'col_4' as which, col_4 as val from table t) union all 
      (select id, col_1, col_2, col_3, col_4, col_5, 'col_5' as which, col_5 as val from table t) 
     ) t 
    ) t 
group by d, col_1, col_2, col_3, col_4, col_5; 

. 그러나 테이블이 너무 크지 않으면 성능은 좋아야합니다.

편집 : 당신이 다섯 개 개의 값이 모두 다르다는 것을 가정하면

, 당신은 할 수 : 값이 NULL 또는 복제 될 수 있다면

select col1, col2, col3, col4, col5, 
     (case when col1 = val1 then 'col1' 
      when col2 = val1 then 'col2' 
      when col3 = val1 then 'col3' 
      when col4 = val1 then 'col4' 
      else 'col5' 
     end) as which1, 
     (case when col1 = val2 then 'col1' 
      when col2 = val2 then 'col2' 
      when col3 = val2 then 'col3' 
      when col4 = val2 then 'col4' 
      else 'col5' 
     end) as which2, 
     (case when col1 not in (val1, val2, val3, val4) then 'col1' 
      when col2 not in (val1, val2, val3, val4) then 'col2' 
      when col3 not in (val1, val2, val3, val4) then 'col3' 
      when col4 not in (val1, val2, val3, val4) then 'col4' 
      else 'col5' 
     end) as which3, 
     (case when col1 = val4 then 'col1' 
      when col2 = val4 then 'col2' 
      when col3 = val4 then 'col3' 
      when col4 = val4 then 'col4' 
      else 'col5' 
     end) as which4, 
     (case when col1 = val5 then 'col1' 
      when col2 = val5 then 'col2' 
      when col3 = val5 then 'col3' 
      when col4 = val5 then 'col4' 
      else 'col5' 
     end) 
from (select least(case when col1 > val1 then col1 else col2 end, 
        case when col2 > val1 then col1 else col1 end, 
        case when col3 > val1 then col1 else col1 end, 
        case when col4 > val1 then col1 else col1 end, 
        case when col5 > val1 then col1 else col1 end 
       ) as val2, 
      least(case when col1 < val5 then col1 else col2 end, 
        case when col2 < val5 then col1 else col1 end, 
        case when col3 < val5 then col1 else col1 end, 
        case when col4 < val5 then col1 else col1 end, 
        case when col5 < val5 then col1 else col1 end 
       ) as val4 
     from (select col1, col2, col3, col4, col5, 
        least(col1, col2, col3, col4, col5) as val1, 
        greatest(col1, col2, col3, col4, col5) as val5 
      from table t 
      ) t 
    ) t 

이 방법이 더 복잡해진다.

+0

고든에게 감사드립니다. 테이블에는 700,000 개의 행이 있으므로 성능이 걱정됩니다. 이 코드를 작성하여 살펴 보겠습니다. 감사합니다 – BarJames

+0

@BarJames. . . 그렇게 나쁘지 않아야합니다. "within-row"메소드를 작동 시키는데 필요한'case' 문을 모두 코딩하기 전에 반환 할 것이라고 확신합니다. –

+0

그래서 그것은 대접했습니다! 저는 UNION을 사용했던 두 곳 중 첫 번째를 사용했습니다. 고맙습니다. – BarJames