Как получить вывод Query с разными полями - PullRequest
0 голосов
/ 04 февраля 2019
  source   | destination | totalkms >
-----------+-------------+----------
 chennai   | bangalore   |      400
 bangalore | chennai     |      400
 mumbai    | delhi       |     1400
 delhi     | mumbai      |     1400
 delhi     | patna       |      800

ожидаемый результат

  source   | destination | totalkms 
  ---------+-------------+----------
 chennai   | bangalore   |      400
 mumbai    | delhi       |     1400
 delhi     | patna       |      800

Ответы [ 2 ]

0 голосов
/ 04 февраля 2019

Вы можете попробовать наименьшее () и наибольшее () методы с предложением group by, как показано ниже.

select least(source, destination),greatest(source, destination),max(totalkms) from test_travel group by least(source, destination),greatest(source, destination);

0 голосов
/ 04 февраля 2019

Вы можете использовать not exists и union all:

select t.*
from t
where t.source < t.destination
union all
select t.*
from t
where t.source > t.destination and
      not exists (select 1
                  from t t2
                  where t2.source = t.destination and t2.destination = t.source and
                        t2.totalkms = t.totalkms
                 );
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...