2

I have a Sql Statement which its result is something like following

  TagNo1         TagNo2
 ---------------------------
   63516         63546
   63546         63516

How can set it that I need just one record . For example

    TagNo1         TagNo2
 ---------------------------
     63516         63546

or the other, no difference. because it is the same for me?

Thank you

1
  • just add TagNo1 > TagNo2 condition into the SQL Commented Feb 21, 2018 at 8:28

2 Answers 2

4

Return a row if TagNo1 is less or equal to TagNo2. Or if the switched values don't exist.

select distinct TagNo1, TagNo2
from tablename t1
where TagNo1 <= TagNo2
   or not exists (select 1 from tablename t2
                  where t1.TagNo1 = t2.TagNo2
                    and t1.TagNo2 = t2.TagNo1)

Do select distinct to avoid duplicates if TagNo1 = TagNo2.

Or use case expressions:

select distinct case when TagNo1 <= TagNo2 then TagNo1 else TagNo2 end,
                case when TagNo1 >= TagNo2 then TagNo1 else TagNo2 end
from tablename
Sign up to request clarification or add additional context in comments.

Comments

1
declare @t table (col1 int, col2 int);
insert into @t values (63516, 63546), (63546, 63516);

select col1, col2 
from @t 
where col1 <= col2 
union 
select col2, col1 
from @t 
where col2 < col1 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.