declare @Tmp table(a int,b int,c int,d int,bh int IDENTITY (1, 1))
insert @Tmp select 5, 6, 5, 6
union all select 5, 6, 7, 8
union all select 5, 2, 4, 2
select distinct bh,a from (select bh,a from @tmp union all
select bh,b from @tmp union all
select bh,c from @tmp union all
select bh,d from @tmp) abc
declare @a table(id int identity,A int,B int,C int,D int)
insert @a select 1, 2, 1, 2
union all select 1, 2, 3, 4
select id,v from (
select id,1 as f,A as V from @a
union all
select id,2 as f,b as V from @a
union all
select id,3 as f,C as V from @a
union all
select id,4 as f,d as V from @a ) T1
where not exists (select 1 from (
select id,1 as f,A as V from @a
union all
select id,2 as f,b as V from @a
union all
select id,3 as f,c as V from @a
union all
select id,4 as f,d as V from @a ) T2
where id=T1.id and F<T1.F and V=T1.V )
order by id,F
declare @Tmp table(a int,b int,c int,d int,e int,bh int IDENTITY (1, 1))
insert @tmp select 1,2,1,2,1
insert @tmp select 1,2,3,4,5
select bh,a from @tmp union all
select bh,b from @tmp union all
select bh,c from @tmp union all
select bh,d from @tmp union all
select bh,e from @tmp order by bh