SQL 飞行路径组合去重:Same Origin + '-' + Destination & Destination + '-' + Origin must be treated as duplicate

SQL de-duplication of flight path combinations: Same Origin + '-' + Destination & Destination + '-' + Origin must be treated as duplicate

我正在想办法破解这个要求。这是航班 table

Origin Destination
LAX PDX
NYC SEA
PDX LAX
SEA NYC

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

您将按串联逻辑分组

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