|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
pivot table with T-SQL
Hallo,
I have a table with 3 columns: table tbl_user_class =============== user_id (int) PK class_id (varchar(20)) PK class_value(varchar(100)) values are like so: user_id class_id class_value ====================== 1 firstname Rogier 1 lastname Doekes 2 firstname Mary 3 lastname Smith ..... I would like to create result set in the following format user_id firstname lastname ==================== 1 Rogier Doekes 2 Mary Smith ...... How do I accomplish this? Thanks for any help,
__________________
- Rogier Doekes |
|
#2
|
|||
|
|||
|
solution
Hi ,
Your problem can be solved in the following way. I am pasting my code for this below. Create table #tmpTest11(userID int,classid varchar(20),classvalue varchar(50)) Insert into #tmpTest11 values(1,'firstname','Rogier') Insert into #tmpTest11 values(1,'lastname','Doekes') Insert into #tmpTest11 values(2,'firstname','Mary') Insert into #tmpTest11 values(2,'lastname','Smith') --Your required result select TT.userID,classvalue as firstname,T.lasttname from #tmpTest11 TT inner join (select userID,classvalue as lasttname from #tmpTest11 where classid='lastname') as T on TT.classid='firstname' and TT.userID=T.userID I hope this helps you. |
![]() |
| Viewing: Dev Articles Community Forums > Databases > Microsoft SQL Server > pivot table with T-SQL |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|