Click here to Skip to main content
15,911,707 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have two tables like this (Used by another program, so cant change the structure)
Table Name: User

UserId       UserName        DisplayName
-----------------------------------------
1            AAA             AAA
2            BBB             BBB
3            CCC             CCC


Table Name: User Profile

UserId       Property        Value
-----------------------------------------
1            Age             21
1            Address         Address of AAA
1            Email           aaa@mycompany.com 
2            Age             25
2            Address         Address of BBB
2            Email           bbb@mycompany.com 
3            Age             28
3            Address         Address of CCC
3            Email           ccc@mycompany.com 


What I want is to get the Table as like this using a query

UserId      UserName     DisplayName     Age    Address             Email
-----------------------------------------------------------------------------------
1           AAA          AAA             21     Address of AAA     aaa@mycompany.com
2           BBB          BBB             25     Address of BBB     bbb@mycompany.com
3           CCC          CCC             28     Address of CCC     ccc@mycompany.com


Can anybody please help me ?

Thanks
Posted
Comments
Christian Graus 12-Jan-14 18:38pm    
This sort of DB design is always a nightmare. Can you change it ? It's a common antipattern.
Yesudasan Moses 13-Jan-14 2:26am    
Its actually the User Profile table of DotnetNuke...
and, I am trying to show a custom report of users,,,,

SQL
SELECT u.UserId, UserName, DisplayName,
(SELECT u2.Value FROM [User Profile] u2 WHERE u2.Property="Age" AND u2.UserId = u.UserId) AS Age,
(SELECT u3.Value FROM [User Profile] u3 WHERE u3.Property="Address" AND u3.UserId = u.UserId) AS Address,
(SELECT u4.Value FROM [User Profile] u4 WHERE u4.Property="Email" AND u4.UserId = u.UserId) AS Email
FROM User u
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 12-Jan-14 7:42am    
5!tested. working excellent..
Try:
SQL
SELECT u.[UserId], u.[UserName], u.[DisplayName], pa.Value as Age, pd.Value as Address, pe.Value as Email FROM [User] u
JOIN UserProfile pa ON pa.UserId=u.USerId AND pa.Property='Age'
JOIN UserProfile pd ON pd.UserId=u.USerId AND pd.Property='Address'
JOIN UserProfile pe ON pe.UserId=u.USerId AND pe.Property='Email'
 
Share this answer
 
Comments
Karthik_Mahalingam 12-Jan-14 7:41am    
5! working excellent..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900