Bad move! Why do you even consider denormalizing your table?-- prepare test datadeclare @test table (Id int, TypeId int, Name varchar(100))insert @testselect 1, 1, 'a' union allselect 15, 1, 'e' union allselect 6, 2, 'f' union allselect 3, 1, 'b' union allselect 8, 2, 'j' union allselect 99, 1, 'q' union allselect 10, 2, 'g' union allselect 2, 1, 'd' union allselect 8, 3, 'h' union allselect 17, 1, 'c' union allselect 11, 2, 'u' union allselect 4, 3, 'i'-- prepare result tabledeclare @result table (Id int, Typeid int, Name1 varchar(100), Name2 varchar(100), Name3 varchar(100))-- do the workdeclare @currenttypeid int, @maxtypeid int, @n1 varchar(100), @n2 varchar(100), @n3 varchar(100), @loop intselect @currenttypeid = min(typeid), @maxtypeid = max(typeid), @loop = 1from @testwhile @currenttypeid <= @maxtypeid begin select @n1 = min(name) from @test where name > isnull(@n3, '') and typeid = @currenttypeid select @n2 = min(name) from @test where name > isnull(@n1, '') and name > isnull(@n3, '') and typeid = @currenttypeid select @n3 = min(name) from @test where name > isnull(@n2, '') and name > isnull(@n1, '') and name > isnull(@n3, '') and typeid = @currenttypeid if @n1 is not null begin select @loop = @loop + 1 insert @result select @loop, @currenttypeid, @n1, @n2, @n3 end IF @n3 IS NULL SELECT @currenttypeid = min(typeid) from @test where typeid > @currenttypeid endselect * From @result
Peter LarssonHelsingborg, Sweden