You need to sort origin and destination first, to make the pairs with the same end points equal. As it's only two columns, you can do that in a CASE ... END
here. Then you can GROUP BY
these and get the count(*)
.
SELECT CASE
WHEN origin <= destination
THEN origin
ELSE
destination
END,
CASE
WHEN destination >= origin
THEN destination
ELSE
origin
END,
count(*)
FROM routes
GROUP BY CASE
WHEN origin <= destination
THEN origin
ELSE
destination
END,
CASE
WHEN destination >= origin
THEN destination
ELSE
origin
END;