SQL Server Prevent double matches on self referencing table

qlzsbp2j  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(91)

Is there a quick and simple way in SQL to accomplish this, I have drawn up a long way to prevent this, by doing a "nested" self reference. Is there a simpler way?

Issue:
When you are looking for similarities in table column you get [ID1, ID2] and [ID2, ID1] when you really only want one of them.

Example:

SELECT * 
FROM Table t1
INNER JOIN Table t2 ON t1.col = t2.col
WHERE t1.id <> t2.id

Result:

t1.idt2.idt1.colt2.col
13sushisushi
31sushisushi
24tacotaco
42tacotaco

Desired result:

t1.idt2.idt1.colt2.col
13sushisushi
24tacotaco

相关问题