SQL不同字段组合忽略位置

时间:2017-04-26 23:13:27

标签: sql sql-server

您好我有一个SQL问题:

表:航班

Origin  Destination
--------------------
Boston    LA
LA        Bostion
Newyork   LA
DC        Newyork

如果我不关心起源或目的地,我如何选择战斗路径的独特组合:(波士顿到洛杉矶)=(洛杉矶到波士顿)

10 个答案:

答案 0 :(得分:2)

您可以使用row_number执行此操作。获取至少原点和目的地的分区,最大的起点和终点,因此像(a,b),(b,a)这样的组合被视为相同。然后获得每组的第一行。

select origin,destination from (
select origin,destination
,row_number() over(partition by 
                   case when origin<destination then origin else destination end,
                   case when origin>destination then origin else destination end
                   order by origin) as rnum
from flight
) t
where rnum=1

答案 1 :(得分:2)

找到独特的飞行路线的最简单方法

Select  distinct case when origin < destination then origin else destination end as Distinct_departure,
                case when origin < destination then destination else origin end as Distinct_arrival
From flights

答案 2 :(得分:1)

这样做你想要的吗?

select origin, destination
from flights f
where origin < destination
union all
select destination, origin
from flights f
where origin > destination and
      not exists (select 1
                  from flights f2
                  where f2.destination = f.origin and f2.origin = f.destination
                 );

答案 3 :(得分:1)

Row_Number() 在两条记录的来源相同但目的地不同的情况下不正确。在这种情况下,它将被分配错误的等级。更好的方法是使用 RANK() 或 DENSE_RANK() 函数。

select origin,destination from (
select origin,destination
             ,DENSE_RANK() over(partition by 
               case when origin<destination then origin else destination end,
               case when origin>destination then origin else destination end
               order by origin) as rnum
 from flight
 ) t
 where rnum=1

答案 4 :(得分:0)

请尝试以下方法......

SELECT CASE
           WHEN Origin < Destination THEN
               Origin
           ELSE
               Destination
       END AS Terminus1,
       CASE
           WHEN Origin < Destination THEN
               Destination
           ELSE
               Origin
       END AS Terminus2
FROM Flight
GROUP BY Terminus1,
         Terminus2
ORDER BY Terminus1,
         Terminus2;

CASE用于水平排序每条记录,以便首先返回每条记录中两个值中的“较小”。

然后

GROUP BY将消除水平排序结果中的重复记录。

在这种情况下不需要

ORDER BY,但我认为它可能会使结果更具可读性。如果您在一组可能在其他地方处理而不是由人类阅读的结果之后,您可能希望转储此条款。

如果您有任何问题或意见,请随时发表评论。

答案 5 :(得分:0)

为什么这样做不那么简单

SELECT origin, destination 
FROM flights
UNION -- Will remove duplicates
SELECT destination, origin
FROM flights;

答案 6 :(得分:0)

select distinct a1.f1,a1.f2 from
(select a.*,row_number() over() rn
from al_ofs_maps.flights a) a1
inner join 
(select a.*,row_number() over() rn
from al_ofs_maps.flights a) a2
on a1.f1=a2.f2 and a1.f2=a2.f1
and a1.rn < a2.rn
union
select distinct  a.*
from al_ofs_maps.flights a
left outer join al_ofs_maps.flights b
on a.f1 = b.f2 and a.f2=b.f1
where b.f1 is null

答案 7 :(得分:0)

试试看!为我工作:

select departure, arrival 
from flight f1, flight f2
where f1.departure != f2.arrival
group by f1.arrival;

答案 8 :(得分:0)

最简单的 SQL 查询是 -

SELECT DISTINCT
    least(Origin , Destination) AS C1,
    greatest(Origin , Destination) AS C2 
FROM Flight;

注意 - 在 MySQL 上测试

答案 9 :(得分:-1)

SELECT * FROM flights 
WHERE origin < destination