嗨我有一个问题,我有一张桌子如下:
from_city to_city Distance
bangalore chennai 350
chennai Bangalore 350
mumbai bangalore 500
bangalore Mumbai 500
Mumbai Delhi 500
Delhi Mumbai 500
我可以在这里得到如下答案:
bangalore chennai 350
Mumbai Bangalore 500
Mumbai Delhi 500
答案可以是bangalore-chennai或channai-bangalore。因为两种方式都有相同的距离。我每个城市只需要一个入口。我怎么能实现它?答案应该是一般性的。 tommorrrow如果添加其他城市像bangalore -mysore或mysore-bangalore,我只需要输入。
答案 0 :(得分:1)
一种方法是使用distinct与case:
select distinct
case when lower(from_city) < lower(to_city)
then Initcap(from_city) else Initcap(to_city) end as from_city,
case when lower(from_city) < lower(to_city)
then Initcap(to_city) else Initcap(from_city) end as to_city,
Distance
from t;
产地:
FROM_CITY TO_CITY DISTANCE
--------------------------------
Delhi Mumbai 500
Bangalore Mumbai 500
Bangalore Chennai 350