sql unique records puzzle
sql, sql-server
Solution
select distinct
case when a<b then a else b end
,case when a<b then b else a end
from @t
;
Problem
The puzzle is to select unique pairs. Syntax in the following example is for Mssql ``` declare @t table (a int, b int) insert into @t (a,b) values (1,2) insert into @t (a,b) values (2,1) insert into @t (a,b) values (1,3) insert into @t (a,b) values (3,1) insert into @t (a,b) values (5,6) select * from @t -- it outputs 5 records. ``` I need to get unique pairs only regardless of the order a,b, which should give me three records Output should be ``` (1,2),(1,3),(5,6) ``` I am out of ideas and will appreciate the help:)