October 31, 2010 at 2:38 pm
Hi I am trying to load a table to another database. I know that i have to apply pivot to transform the data, but am just not able to figure out how.
A sample of my data is given
idtitle4 value
63Name XYZ
63RollNo 1011190
63DateOfBirth14/12/84
63GreadStreamABCD
63Cf_gradInstituePQRS
63cf_gradpercent93.5
i want the data such that i get the records in title4 as columns and corresponding value in the rows
ID Name RollNo DateOfBirth GreadStream Cf_gradInstitue cf_gradpercent
63 XYZ 1011190 14/12/84 ABCD PQRS 93.5
any help will be much appreciated
October 31, 2010 at 3:50 pm
Hi there Hussain,
Try something like this
-- Generate testdata
DECLARE @T TABLE (id int, title4 varchar(20), value varchar(50))
INSERT @T (id, title4, value)
VALUES(63, 'Name', 'XYZ')
INSERT @T (id, title4, value)
VALUES(63, 'RollNo', '1011190')
INSERT @T (id, title4, value)
VALUES(63, 'DateOfBirth', '14/12/84')
INSERT @T (id, title4, value)
VALUES(63, 'GreadStream', 'ABCD')
INSERT @T (id, title4, value)
VALUES(63, 'Cf_gradInstitue', 'PQRS')
INSERT @T (id, title4, value)
VALUES(63, 'cf_gradpercent', '93.5')
-- Pivot
SELECT ID, Name, RollNo, DateOfBirth, GreadStream, Cf_gradInstitue, cf_gradpercent
FROM (
SELECT ID, title4, value
FROM @T) up
PIVOT (max(Value) FOR title4 IN (Name, RollNo, DateOfBirth, GreadStream, Cf_gradInstitue, cf_gradpercent)) AS pvt
ORDER BY ID
Andreas Goldman
October 31, 2010 at 8:05 pm
Hi Andreas
This was just what I required. Thank you very much
Regards
Hussain
November 1, 2010 at 1:37 pm
You're welcome. Glad I could help.
Andreas Goldman
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply