One approach is to use CASE
expressions to find the least/greatest value in each record:
SELECT DISTINCT
CASE WHEN ColumnA < ColumnB THEN ColumnA ELSE ColumnB END AS ColumnA,
CASE WHEN ColumnA >= ColumnB THEN ColumnA ELSE ColumnB END AS ColumnB
FROM yourTable
ORDER BY 1;
Demo
Note that in certain other databases (such as MySQL and Postgres), there are formal LEAST
and GREATEST
functions, which can simplify and eliminate the need for the bulky CASE
expressions used above.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…