简体   繁体   English

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

[英]I have table as shown below with source,Target and KMS columns and i need out based on KMS columns

I need the output based on KMS. 我需要基于KMS的输出。 Table data and expected output: 表数据和预期输出:

在此处输入图片说明

  1. if KMS is same and source and Target is same(1st and 2nd record are same logically) then consider it as one record 如果KMS相同并且源和目标相同(第一条记录和第二条记录在逻辑上相同),则将其视为一条记录

  2. If the KMS is same and source and Target is different consider it as different record(consider the last record it has same KMS as 1st record but it different record) 如果KMS相同且源和目标不同,则将其视为不同的记录(考虑最后一条记录,其KMS与第一条记录相同,但其记录不同)

在此处输入图片说明

If we consider consider each record as a pair and then sort and transpose the source and target accordingly, you should be able to pick out what records go together. 如果我们考虑将每条记录视为一对,然后对源和目标进行分类和转置,那么您应该能够挑选出哪些记录在一起。

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;

The solution does not work for NULLS. 该解决方案不适用于NULL。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有一个表格,其中包含国家和城市列,如下表输入所示。 我需要如下所述的 output - There is a table having country and city columns as shown in the below table input. I need the output as mentioned below 我有两列需要解析的多个值 - I have two columns with multiple values that I need to parse out SQL 问题我有一个如下所示的表需要像 ba, ba, ba 这样的输出 - SQL question I have a table shown below need output like ba, ba, ba 我需要想办法在我的显示器上显示它。 我有2列,我需要能够显示 - I need to figure out a way to display this on my. I have 2 columns, i Need to be able to display 在我的源中有两列:A 和 B,在我的目标中有三列:A、B 和 C,我们必须使用源在 c 列中生成整数值 - In my source I have two columns: A and B, In my target I have three columns: A, B and C and we have to generate integer value in c colum using source Sql查询 - 我在一个表中有三列,我需要将每列数据检查到另一个表中 - Sql query - I have three columns in one table and I need to check each column data into another table 如何将结果集行显示为列 - How can I have resultset rows be shown as columns 我有一张桌子,我需要在特定日期范围内对2列进行分组和计数 - I have a table where i need to group and count 2 columns within a certain date range 我现在有一个包含不同部门的示例表,该场景需要在所有列的表中显示重复率最高的部门 - I have a sample table with distinct departments now the scenario is,need to display the highest repeated departments in a table with all columns 基于从源表的一列插入到目标表的多列中来优化查询 - Optimizing Query based on Inserting into multiple columns into target table from one column of a source table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM