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 |
ugh3012
Yak Posting Veteran
62 Posts |
Posted - 2013-12-24 : 10:23:35
|
I have to pivot one table to get the result in correct format. Here is an example as I am not sure how to explain it. If there is another way instead of pivot, let me know. Table1ID | Name |1 | Joe |2 | Ron |Table2Table1_ID | Language 1 | English1 | ASL2 | Spanish2 | English2 | French I need the two tables joined and table2 to be pivoted to get the desired result as shown below. Third column for language is left off as I am limited to two columns. ResultID | Name | Language 1| Language 21 | Joe | English | ASL2 | Ron | Spanish | English |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-12-24 : 12:40:53
|
[code]SELECT ID,Name,[1] AS Language1,[2] AS Language2FROM(SELECT t1.ID,Name,[Language],RnFROM Table1 t1INNER JOIN (SELECT ROW_NUMBER() OVER (PARTITION BY table1_ID ORDER BY [Language]) AS Rn,*FROM Table2)t2ON t2.Table1_ID = t1.IDAND t2.Rn <=2)mPIVOT (MAX([Language]) FOR Rn IN ([1],[2]))p[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
cgraus
Starting Member
12 Posts |
Posted - 2013-12-27 : 05:06:38
|
Note that one of your users has three values, and both the pivot solution, and the one I am offering, will only find as many values as you write code for. Here's a way of doing it without pivots.SELECT id, name, MAX(CASE WHEN t.row = 1 THEN language ELSE NULL END) as language1, MAX(CASE WHEN t.row = 2 THEN language ELSE NULL END) as language2, MAX(CASE WHEN t.row = 3 THEN language ELSE NULL END) as language3 FROM( select id, name, language, row_number() OVER (partition by id order by name) as row FROM table1 t1 inner join table2 t2 on t1.id = t2.table1_id ) tGROUP BY id, name |
|
|
cgraus
Starting Member
12 Posts |
Posted - 2013-12-27 : 05:09:10
|
And here's a better way. This uses FOR XML to build a comma seperated list. It will work for any number of records for the same user.SELECT id, name, STUFF((Select ',' + CAST(language AS VARCHAR(1000)) FROM table2 t2 WHERE t1.id = t2.table1_id FOR XML PATh ('') ),1,1,'')from table1 t1 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-12-28 : 01:52:38
|
quote: Originally posted by cgraus And here's a better way. This uses FOR XML to build a comma seperated list. It will work for any number of records for the same user.SELECT id, name, STUFF((Select ',' + CAST(language AS VARCHAR(1000)) FROM table2 t2 WHERE t1.id = t2.table1_id FOR XML PATh ('') ),1,1,'')from table1 t1
This will just give all language values inside same column which is not what OP is asking for------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-12-28 : 01:54:23
|
quote: Originally posted by cgraus Note that one of your users has three values, and both the pivot solution, and the one I am offering, will only find as many values as you write code for. Here's a way of doing it without pivots.SELECT id, name, MAX(CASE WHEN t.row = 1 THEN language ELSE NULL END) as language1, MAX(CASE WHEN t.row = 2 THEN language ELSE NULL END) as language2, MAX(CASE WHEN t.row = 3 THEN language ELSE NULL END) as language3 FROM( select id, name, language, row_number() OVER (partition by id order by name) as row FROM table1 t1 inner join table2 t2 on t1.id = t2.table1_id ) tGROUP BY id, name
Even if you want to extend it for unknown number of values you can use methods like below which will obviously involve dynamic sqlhttp://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/dynamic-crosstab-with-multiple-pivot-columns.aspxhttp://beyondrelational.com/modules/2/blogs/70/posts/10840/dynamic-pivot-in-sql-server-2005.aspx------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|