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 |
|
bendertez
Yak Posting Veteran
94 Posts |
Posted - 2008-09-16 : 07:08:08
|
| HelloI have an employee course table storing employee course data:Emp_ref | Course0000123 | Net L10000123 | Dev L10000123 | H&S L10000134 | PM L20000134 | CM L20000167 | H&S L10000167 | CM L20000187 | H&S L2As you can see employees can be on more than one course at a time.What I need to do is convert the course data in to individual rows so I can export it in to another database.The format needs to be as follows:emp_ref | Course1 | Course2 | Course30000123 | Net L1 | Dev L1 | H&S L10000134 | PM L2 | CM L2 0000167 | H&S L1 | CM L2 0000187 | H&S L2 Can anyone point me in the right direction as to how this might be achieved, if indeed it can be?Thank you |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-16 : 07:32:57
|
| [code]SELECT t.Emp_Ref,MAX(CASE WHEN t.Seq=1 THEN Course ELSE NULL END) AS Course1,MAX(CASE WHEN t.Seq=2 THEN Course ELSE NULL END) AS Course2,MAX(CASE WHEN t.Seq=3 THEN Course ELSE NULL END) AS Course3FROM(SELECT ROW_NUMBER() OVER(PARTITION BY Emp_Ref ORDER BY Course) AS Seq,*FROm YourTable)tGROUP BY t.Emp_Ref[/code] |
 |
|
|
ranganath
Posting Yak Master
209 Posts |
Posted - 2008-09-16 : 09:12:49
|
| Hi,try with this alsoCreate Table #Emp (Emp_ref Varchar(100), Course Varchar(100))Insert into #Emp (Emp_ref, Course)Select '0000123' , 'Net L1' Union AllSelect '0000123' , 'Dev L1' Union AllSelect '0000123' , 'H&S L1' Union AllSelect '0000134' , 'PM L2' Union AllSelect '0000134' , 'CM L2' Union AllSelect '0000167' , 'H&S L1' Union AllSelect '0000167' , 'CM L2' Union AllSelect '0000187' , 'H&S L2'Select * , 'Course' as Lang, Row_Number() over(Partition by Emp_ref order by Emp_ref) as 'Rank' into #T From #Emp --Select * From #TDeclare @Str VArchar(8000), @Sql Varchar(8000)Set @Sql = ''Set @Str = ''Select @Sql = @Sql + ', Min(Case when Lang = ''' + Lang + ''' and rank = '''+cast([rank] as varchar(100))+'''Then Course End) AS "' + Lang + ' '+ cast([Rank] as varchar(100))+' "'From (Select distinct Lang , [Rank] From #T )ASet @Str = 'Select Emp_ref '+@sql+' From #T Group By Emp_ref'Exec (@Str)Drop Table #Emp, #T |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-16 : 09:24:36
|
quote: Originally posted by ranganath Hi,try with this alsoCreate Table #Emp (Emp_ref Varchar(100), Course Varchar(100))Insert into #Emp (Emp_ref, Course)Select '0000123' , 'Net L1' Union AllSelect '0000123' , 'Dev L1' Union AllSelect '0000123' , 'H&S L1' Union AllSelect '0000134' , 'PM L2' Union AllSelect '0000134' , 'CM L2' Union AllSelect '0000167' , 'H&S L1' Union AllSelect '0000167' , 'CM L2' Union AllSelect '0000187' , 'H&S L2'Select * , 'Course' as Lang, Row_Number() over(Partition by Emp_ref order by Emp_ref) as 'Rank' into #T From #Emp --Select * From #TDeclare @Str VArchar(8000), @Sql Varchar(8000)Set @Sql = ''Set @Str = ''Select @Sql = @Sql + ', Min(Case when Lang = ''' + Lang + ''' and rank = '''+cast([rank] as varchar(100))+'''Then Course End) AS "' + Lang + ' '+ cast([Rank] as varchar(100))+' "'From (Select distinct Lang , [Rank] From #T )ASet @Str = 'Select Emp_ref '+@sql+' From #T Group By Emp_ref'Exec (@Str)Drop Table #Emp, #T
why use dynamic sql when number of columns required is static? |
 |
|
|
ranganath
Posting Yak Master
209 Posts |
Posted - 2008-09-16 : 09:35:10
|
| hiif you don't know the number of columns exactly use dynamic otherwise statically |
 |
|
|
bendertez
Yak Posting Veteran
94 Posts |
Posted - 2008-09-16 : 09:35:33
|
| Hi GuysThanks for the posts, Visakh's method has worked a treat.Thanks again |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-16 : 09:41:56
|
quote: Originally posted by bendertez Hi GuysThanks for the posts, Visakh's method has worked a treat.Thanks again
You're welcome |
 |
|
|
|
|
|
|
|