TableA
------
UserID | UserName | Password
----------------------------
1 Test1 Pass
2 Test2 Pass
3 Test3 Pass
TableB
------
UserDetailID | UserID | FieldName | FieldValue
----------------------------------------------
1 1 Address1 #24, 2nd Main
2 1 Address2 Near to abc
3 1 City Chennai
4 1 State Tamilnadu
5 2 Address1 #45, 3rd Main
6 2 Address2 Near to XYZ
7 2 City Bangalore
8 2 State Karnataka
I need a pivot query which can convert this two table as below
UserID | UserName | Password | Address1 | Address2 | City | State
---------------------------------------------------------------------------
1 Test1 Pass #24, 2nd Main Near to ABC Chennai Tamilnadu
2 Test2 Pass #45, 3rd Main Near to XYZ Bangalore Karnataka
How i can achieve this?
Asked
Active
Viewed 34 times
0

lak
- 11
- 1
- 4
-
Are the `FieldName`s fixed? – Felix Pamittan Jul 14 '15 at 05:13
-
Fields are dynamic in TableB, It may also include Address3 and Address4 – lak Jul 14 '15 at 05:16
-
You can do this using dynamic crosstab. Check my answer [**here**](http://stackoverflow.com/a/28469673/2203084). – Felix Pamittan Jul 14 '15 at 05:26
-
@wewesthemenace even though we provided examples if this newbie unable to get the requirement done what should he do ?? – mohan111 Jul 14 '15 at 05:47
-
@lak have you got your answer by the above provided link or still you need assistance – mohan111 Jul 14 '15 at 05:48
-
@mohan111 - Even though he/she is newbie he/she should put some effort this is a very basic question!!. There are thousands of answers present in internet for such questions – Pரதீப் Jul 14 '15 at 05:50
-
http://stackoverflow.com/questions/15931607/convert-rows-to-columns-using-pivot-in-sql-server http://stackoverflow.com/questions/15745042/efficiently-convert-rows-to-columns-in-sql-server/15745076#15745076 http://stackoverflow.com/questions/10404348/sql-server-dynamic-pivot-query/10404455#10404455 – Pரதீப் Jul 14 '15 at 05:53
-
yeah @Fireblade i agree with you mate – mohan111 Jul 14 '15 at 05:53
-
thanks everyone, i got the answer, thanks for the reference links – lak Jul 14 '15 at 07:31