在一行内查找并保持唯一值

时间:2019-03-06 13:39:17

标签: sql oracle

希望您可以提供帮助。

我有一个像这样的数据集:

Starting Table

我想添加保留给定变量的前两个唯一值(如果适用)的列,如下所示:

enter image description here

谢谢!

1 个答案:

答案 0 :(得分:2)

这假定您不希望对null进行任何特殊处理。

select indiv_id, trip_band, 
        group_shot_code_1 as final_group1,
         case
      when  group_shot_code_2 != group_shot_code_1 then group_shot_code_2
      when  group_shot_code_3 != group_shot_code_1 then group_shot_code_3
       when  group_shot_code_4 != group_shot_code_1 then group_shot_code_4
       when  group_shot_code_5 != group_shot_code_1 then group_shot_code_5
    end as final_group2
From your_table;

我也无法对其进行测试,但是看起来还可以。让我知道是否存在无法修复的语法错误。

相关问题