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.
| Author |
Topic |
|
loken.bhatt
Starting Member
5 Posts |
Posted - 2007-03-28 : 02:52:58
|
| Hello AllI want to Split multirow resultset to single row, multicolumn resultsetmy table isProfileDetail(ProfileId bigint, PropertyId bigint PropertyValue nvarchar(400))Table data :ProfileId PropertyId PropertyValue97 1 lokendra97 2 bhatt97 3 m97 4 Oct 23 1987 12:00AM97 5 2344 97 6 53532497 7 197 8 197 9 xyz97 10 0I put a Query to retrive data from multiple row to single row :select b.PropertyValue as FName,(select b.PropertyValue from (select ProfileId,PropertyValue,Row_Number() over( order by ProfileDetail.ProfileId ) as RowId from ProfileDetail (Nolock)) b where b.rowid=2 ) as Lname ,(select b.PropertyValue from (select ProfileId,PropertyValue,Row_Number() over( order by ProfileDetail.ProfileId ) as RowId from ProfileDetail (Nolock)) b where b.rowid=3 ) as Gender ,(select b.PropertyValue from (select ProfileId,PropertyValue,Row_Number() over( order by ProfileDetail.ProfileId ) as RowId from ProfileDetail (Nolock)) b where b.rowid=4 ) as BDate ,(select b.PropertyValue from (select ProfileId,PropertyValue,Row_Number() over( order by ProfileDetail.ProfileId ) as RowId from ProfileDetail (Nolock)) b where b.rowid=5 ) as Zip ,(select b.PropertyValue from (select ProfileId,PropertyValue,Row_Number() over( order by ProfileDetail.ProfileId ) as RowId from ProfileDetail (Nolock)) b where b.rowid=6 ) as Phone ,(select b.PropertyValue from (select ProfileId,PropertyValue,Row_Number() over( order by ProfileDetail.ProfileId ) as RowId from ProfileDetail (Nolock)) b where b.rowid=7 ) as GMTZoneId from (select ProfileId,PropertyValue,Row_Number() over( order by ProfileDetail.ProfileId ) as RowIdfrom ProfileDetail (Nolock) where ProfileID=97) b where RowId=1it return the required result ouput :Fname Lname Gender Bdate Zip Phone GmtZoneIdhdg bhatt m Oct 23 1987 53532 454 1Question : What I need to tune the above query so that it can give me faster result or any idea to get the same result set with different queryThnksLokendra |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-28 : 03:02:32
|
| [code]-- Prepare sample dataDECLARE @ProfileDetail TABLE (ProfileID INT, PropertyID INT, PropertyValue VARCHAR(40))INSERT @ProfileDetailSELECT 97, 1, 'lokendra' UNION ALLSELECT 97, 2, 'bhatt' UNION ALLSELECT 97, 3, 'm' UNION ALLSELECT 97, 4, 'Oct 23 1987 12:00AM' UNION ALLSELECT 97, 5, '2344' UNION ALLSELECT 97, 6, '535324' UNION ALLSELECT 97, 7, '1' UNION ALLSELECT 97, 8, '1' UNION ALLSELECT 97, 9, 'xyz' UNION ALLSELECT 97, 10, '0' UNION ALLSELECT 197, 1, 'Peter' UNION ALLSELECT 197, 2, 'Larsson' UNION ALLSELECT 197, 3, 'm' UNION ALLSELECT 197, 4, 'Last century' UNION ALLSELECT 197, 5, '25462' UNION ALLSELECT 197, 6, 'Unlisted' UNION ALLSELECT 197, 7, '2' UNION ALLSELECT 197, 8, '9' UNION ALLSELECT 197, 9, 'xyz' UNION ALLSELECT 197, 10, 'f'-- Show the expected outputSELECT MAX(CASE WHEN PropertyID = 1 THEN PropertyValue END) AS FName, MAX(CASE WHEN PropertyID = 2 THEN PropertyValue END) AS Lname, MAX(CASE WHEN PropertyID = 3 THEN PropertyValue END) AS Gender, MAX(CASE WHEN PropertyID = 4 THEN PropertyValue END) AS BDate, MAX(CASE WHEN PropertyID = 5 THEN PropertyValue END) AS Zip, MAX(CASE WHEN PropertyID = 6 THEN PropertyValue END) AS Phone, MAX(CASE WHEN PropertyID = 7 THEN PropertyValue END) AS GMTZoneId FROM @ProfileDetail--WHERE ProfileID = 97GROUP BY ProfileIDORDER BY ProfileID[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|
|
|