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
 General SQL Server Forums
 New to SQL Server Programming
 How Denormalize data

Author  Topic 

yaman
Posting Yak Master

213 Posts

Posted - 2008-07-10 : 09:11:19
Sir i have TABLE1
ID Col1 Col2
1 NAME1 A
1 NAME2 B
1 NAME3 c
2 NAME1 D
2 NAME2 E
Then I want Denormalize table and convert into that form FRom query
Like TABLE2
ID NAME1 NAME2 NAME3
1 A B C
2 D E NULL
HOW CAN I CONVERT TABLE1 TO TABLE2 PLS HELP ME OUT SIR


Yaman

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-07-10 : 09:14:48
are you using SQL Server 2005 or 2000 ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

yaman
Posting Yak Master

213 Posts

Posted - 2008-07-11 : 04:11:30
quote:
Originally posted by khtan

are you using SQL Server 2005 or 2000 ?


KH
[spoiler]Time is always against us[/spoiler]





Sir i am using 2005

Yaman
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-07-11 : 04:16:19
[code]DECLARE @TABLE TABLE
(
ID int,
Col1 varchar(5),
Col2 CHAR(1)
)
INSERT INTO @TABLE
SELECT 1, 'NAME1', 'A' UNION ALL
SELECT 1, 'NAME2', 'B' UNION ALL
SELECT 1, 'NAME3', 'c' UNION ALL
SELECT 2, 'NAME1', 'D' UNION ALL
SELECT 2, 'NAME2', 'E'

SELECT *
FROM
(
SELECT *
FROM @TABLE
) d
pivot
(
MAX (Col2)
FOR Col1 IN ([NAME1], [NAME2], [NAME3])
) p

/*
ID NAME1 NAME2 NAME3
----------- ----- ----- -----
1 A B c
2 D E NULL

(2 row(s) affected)
*/
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -