SQL - 列之间的值顺序

时间:2014-07-25 19:22:10

标签: sql oracle

我希望在Oracle SQL中找到5列的值顺序,并将该序列输入第6列。

Col_1, Col_2, Col_3, Col_4, Col_5, Col_6

0.2  , 0.1  , 0.6  , 0.4  , 0.3  , 21543

或者,我可以为值为列名的每一行使用5个输出列。

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

赞赏任何建议。

干杯

1 个答案:

答案 0 :(得分:0)

最简单的方法可能是取消数据,重新编译和提取信息:

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;

这比仅在单行上执行逻辑效率低。但如果表格不是太大,表现应该没问题。

编辑:

如果您认为五个值都不同,您可以这样做:

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

如果值可以是NULL或重复,则会变得更复杂。