SQLTeam.com | Weblogs | Forums

Distinct records on both column

sql2012

#1

i have a table like below.

declare @tbl table (c1 char, c2 char)

insert into @tbl
values('a','b') ,('b','a'),('c','d'),('c','d'),('e','f') ,('g','g')

the scenario is to find out only distinct rows based on both column c1 and c2. no records combination should be
availble in output like a,b and b,a should be considered as duplicate entry and only one entry should be availble in output

when i use distinct then only one side matching is possible but i want to filter out both side duplicacy.

the required output is :
output:

c1 c2
a b
c d
e f
g g


#2
SELECT DISTINCT
	CASE WHEN c1 > c2 THEN c2 ELSE c1 END AS c1
	,CASE WHEN c1 > c2 THEN c1 ELSE c2 END AS c2
FROM @tbl;

#3

i am afraid that this code will fail when there is more than one chars in columns.