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

问题描述 投票:0回答:1

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

enter image description here

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

enter image description here

sql oracle
1个回答
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。

© www.soinside.com 2019 - 2024. All rights reserved.