【问题标题】:SQL de-duplication of flight path combinations: Same Origin + '-' + Destination & Destination + '-' + Origin must be treated as duplicate飞行路径组合的 SQL 去重:Same Origin + '-' + Destination & Destination + '-' + Origin 必须视为重复
【发布时间】:2022-01-05 04:04:27
【问题描述】:

我正试图弄清楚如何解决这个要求。这是飞行表

Origin Destination
LAX PDX
NYC SEA
PDX LAX
SEA NYC

我需要将 LAX-PDX 路线上的数据与 PDX-LAX 路线(不优先选择字符串中的第一个起点或目的地)和与 SEA-NYC 和 NYC-SEA 路线关联的数据分组。你能帮我弄清楚如何处理那些将起点和终点交换为重复的路线,这样我就可以在单条路线上进行 GROUP BY (只要结果是 LAX-PDX 或 PDX-洛杉矶国际机场)?

【问题讨论】:

  • 您可以在 select 和 group by 中以字母顺序较小的一个作为起点,另一个作为目的地
  • 选择 case 当 origin
  • 您介意提供一个所需输出的示例吗?

标签: sql group-by duplicates concatenation


【解决方案1】:

您将按串联逻辑分组

select case when origin> destination then 
            concat(origin,'-',destination)
            else 
            concat(destination,'-',origin)
        end as grp_col
       ,count(*) as no_of_routes
   from table
group by case when origin> destination then 
            concat(origin,'-',destination)
            else 
            concat(destination,'-',origin)
        end
having count(*)>1

【讨论】:

  • 非常感谢您的帮助!它奏效了。
猜你喜欢
  • 2022-08-02
  • 1970-01-01
  • 1970-01-01
  • 2021-05-28
  • 1970-01-01
  • 2021-07-08
  • 2022-01-10
  • 1970-01-01
  • 2020-12-10
相关资源
最近更新 更多