sql - Find distinct rows based on combination of two columns -
i having problem finding solution problem in sql.
say have table this:
id | | -------------------- 4427 | 2 | 3 4427 | 3 | 2 4427 | 3 | 5 4427 | 5 | 3 4427 | 1 | 3 4427 | 2 | 5 4427 | 3 | 1
now want find rows unique combination of id , a. mean want see rows combination of a(1) , a(2) exists once. illustration first 2 rows in table "the same" since combination of 2 , 3 same 3 , 2.
the expected result quesion be:
id | | -------------------- 4427 | 2 | 3 4427 | 3 | 5 4427 | 1 | 3 4427 | 2 | 5
i using sql server 2008.
if understand correctly, simple "distinct" query if had cola value less colb. given this, can utilize case statement handle this, combined distinct operator.
try this.
select distinct id, case when cola < colb cola else colb end, case when cola < colb colb else cola end yourtablehere
sql sql-server sql-server-2008 tsql
No comments:
Post a Comment