我有如下所示的表格,包含源,目标和KMS列,我需要基于KMS列

时间:2018-03-02 07:27:40

标签: sql oracle

我需要基于KMS的输出。表数据和预期产出:

enter image description here

  1. 如果KMS相同且源和目标相同(第一和第二条记录在逻辑上相同),则将其视为一条记录

  2. 如果KMS相同且源和目标不同,则将其视为不同的记录(考虑最后一条记录,它与第一条记录具有相同的KMS但记录不同)

  3. enter image description here

1 个答案:

答案 0 :(得分:0)

如果我们考虑将每条记录视为一对,然后相应地对源和目标进行排序和转置,那么您应该能够找出哪些记录在一起。

with data_from_vijay as(
    select 'Bijapur'    as source, 'Bangalore'  as target, 500 as kms from dual union all
    select 'Bangalore'  as source, 'Bijapur'    as target, 500 as kms from dual union all
    select 'Pune'       as source, 'Bangalore'  as target, 700 as kms from dual union all
    select 'Bangalore'  as source, 'Pune'       as target, 700 as kms from dual union all
    select 'Mangalore'  as source, 'Bangalore'  as target, 400 as kms from dual union all
    select 'Bangalore'  as source, 'Belgaum'    as target, 500 as kms from dual
)
select distinct 
       least(source,    target) as source
      ,greatest(source, target) as target
      ,kms
  from data_from_vijay;

该解决方案不适用于NULLS。