Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Transforming vertical to horizontal data

Author  Topic 

halexic
Starting Member

8 Posts

Posted - 2006-12-10 : 05:33:33
Hello,

I have one problem and don't know what is the best way to do it.

Have one table (Id, TypeId, Name) and another table (Id, TypeName, Name1, Name2, Name3) and I want to copy all vertical data from first table to another table. TypeName is a name from another table for TypeId from first table...
So I need to write tree records from table one to one record in table 2.
If I have in first table 5 records with TypeId=1 then I need to write one record in table two (first 3 records from table 1) and second record in table 2 (4th and 5th record from table 1 plus one empty recored - that mean Name3 will be null).

I am using store procedure and working with temporary tables in memory and using a while syntax and I have done this.
What is the right approach and if you have some similar example I would be greatful.

Thans
Alex

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-12-10 : 14:50:42
select id, typename, name1 as name
union all
select id, typename, name2
union all
select id, typename, name3



Peter Larsson
Helsingborg, Sweden
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-12-10 : 17:25:54
Bad move! Why do you even consider denormalizing your table?
-- prepare test data
declare @test table (Id int, TypeId int, Name varchar(100))

insert @test
select 1, 1, 'a' union all
select 15, 1, 'e' union all
select 6, 2, 'f' union all
select 3, 1, 'b' union all
select 8, 2, 'j' union all
select 99, 1, 'q' union all
select 10, 2, 'g' union all
select 2, 1, 'd' union all
select 8, 3, 'h' union all
select 17, 1, 'c' union all
select 11, 2, 'u' union all
select 4, 3, 'i'

-- prepare result table
declare @result table (Id int, Typeid int, Name1 varchar(100), Name2 varchar(100), Name3 varchar(100))

-- do the work
declare @currenttypeid int,
@maxtypeid int,
@n1 varchar(100),
@n2 varchar(100),
@n3 varchar(100),
@loop int

select @currenttypeid = min(typeid),
@maxtypeid = max(typeid),
@loop = 1
from @test

while @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
end

select * From @result

Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -