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 |
|
generalya
Starting Member
2 Posts |
Posted - 2012-08-21 : 20:24:15
|
| I have two tables as such:ID Name indicator_1 indicator_21 text 1 12 text 1 13 text 1 14 text 2 15 text 2 16 text 2 27 text 2 28 text 3 2FieldName Value Descriptionindicator_1 1 goodindicator_1 2 mediumindicator_1 3 badindicator_2 1 yesindicator_2 2 noIs there any way to join the two tables and arrive at:ID Name indicator_1 indicator_21 text good yes2 text good yes3 text good yes4 text medium yes5 text medium yes6 text medium no7 text medium no8 text bad noThis is just a sample, the actual table has about 70 indicators...thanks much |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-21 : 20:41:48
|
yepyou need to join them separately likeSELECT t1.ID,t1.Name,t2.Description AS indicator_1,t3.Description AS Indicator_2FROM table1 t1INNER JOIN table2 t2ON t2.Value = t1.indicator_1AND t2.FieldName = 'Indicator_1'INNER JOIN table2 t3ON t3.Value = t1.indicator_2AND t3.FieldName = 'Indicator_2' ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-21 : 20:48:03
|
another way isSELECT *FROM(SELECT m.ID, m.Name,m.Category,t2.DescriptionFROM(SELECT *FROM table1 t1UNPIVOT (Value FOR Category IN ([indicator_1],[indicator_2],..))u)mINNER JOIN table2 t2ON t2.FieldName = m.CategoryAND t2.Value = m.Value)pPIVOT (MAX(Description) FOR Category IN ([indicator_1],[indicator_2],..))q ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
generalya
Starting Member
2 Posts |
Posted - 2012-08-22 : 00:23:16
|
Thanks, I am interested in the below query...but newbie question: I don't have a column called category, how does the inner select (UNPIVOT VALUE FOR CATEGORY) work?quote: Originally posted by visakh16 another way isSELECT *FROM(SELECT m.ID, m.Name,m.Category,t2.DescriptionFROM(SELECT *FROM table1 t1UNPIVOT (Value FOR Category IN ([indicator_1],[indicator_2],..))u)mINNER JOIN table2 t2ON t2.FieldName = m.CategoryAND t2.Value = m.Value)pPIVOT (MAX(Description) FOR Category IN ([indicator_1],[indicator_2],..))q ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-22 : 00:32:28
|
| Category column will be created on the fly. you just need to have columns as shown in your sample------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|