| Author |
Topic  |
|
|
rishabhgairola
Starting Member
India
2 Posts |
Posted - 11/20/2012 : 00:46:47
|
i have a table named ColumnField like this :-
ID L1 L2 L3 L4 L5 L6 L7 L8 L9 L10 1 1 3 4 4 7 2 5 2 3 2 2 2 7 5 3 4 3 2 3 1 1 3 3 4 6 4 2 4 4 4 2 4 4 4 3 2 5 7 2 6 5 1 5 5 5 2 3 6 1 4 7 7 4 7
and another table named ColumnText like this :- TextID Text 1 aerosol 2 hydro 3 solar 4 nuclear 5 lunar 6 tidal 7 wind
I need to generate a table where all the fields L1,L2,L3...L10 are replaced with their corresponding texts. Also, the numbers under the various coulmns in the columnField table (L1,L2...L10) correspond to the TextID of the ColumnText table.
Thanks in advance.
|
|
|
TG
Flowing Fount of Yak Knowledge
USA
5469 Posts |
Posted - 11/20/2012 : 01:06:13
|
untested but this should work:
select t1.text as L1
,t2.text as L2
,etc...
from ColumnField f
join ColumnText t1 on t1.textid = f.L1
join columnText t2 on t2.textid = f.L2
etc...
Be One with the Optimizer TG |
 |
|
|
bandi
Flowing Fount of Yak Knowledge
India
1430 Posts |
Posted - 11/20/2012 : 01:16:53
|
Hi TG, your solution is correct
declare @ColumnField table (ID int, L1 int, L2 int, L3 int, L4 int, L5 int, L6 int, L7 int, L8 int, L9 int, L10 int) insert into @ColumnField select 1, 1, 3, 4, 4, 7, 2, 5, 2, 3, 2 union all select 2, 2, 7, 5, 3, 4, 3, 2, 3, 1, 1 union all select 3, 3, 4, 6, 4, 2, 4, 4, 4, 2, 4 union all select 4, 4, 3, 2, 5, 7, 2, 6, 5, 1, 5 union all select 5, 5, 2, 3, 6, 1, 4, 7, 7, 4, 7
declare @ColumnText table(TextID int, [Text] varchar(20)) insert into @ColumnText select 1, 'aerosol' union all select 2, 'hydro' union all select 3, 'solar' union all select 4, 'nuclear' union all select 5, 'lunar' union all select 6, 'tidal' union all select 7, 'wind'
select f.id ,t1.text as L1 ,t2.text as L2 ,t3.text as L3 ,t4.text as L4 ,t5.text as L5 ,t6.text as L6 ,t7.text as L7 ,t8.text as L8 ,t9.text as L9 ,t10.text as L10 from @ColumnField f join @ColumnText t1 on t1.textid = f.L1 join @ColumnText t2 on t2.textid = f.L2 join @ColumnText t3 on t3.textid = f.L3 join @ColumnText t4 on t4.textid = f.L4 join @ColumnText t5 on t5.textid = f.L5 join @ColumnText t6 on t6.textid = f.L6 join @ColumnText t7 on t7.textid = f.L7 join @ColumnText t8 on t8.textid = f.L8 join @ColumnText t9 on t9.textid = f.L9 join @ColumnText t10 on t10.textid = f.L10
-- Chandu |
 |
|
|
TG
Flowing Fount of Yak Knowledge
USA
5469 Posts |
Posted - 11/20/2012 : 01:20:35
|
Thanks for doing the heavy lifting, Chandu! 
Be One with the Optimizer TG |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47136 Posts |
Posted - 11/20/2012 : 02:18:21
|
SELECT ID,[L1],[L2],...,[L10]
FROM
(
SELECT ID,m.Cat,n.Text
FROM
(
SELECT ID,Cat,Val
FROM ColumnField
UNPIVOT (Val FOR Cat IN ([L1],[L2],[L3],...,[L10]))u
)m
INNER JOIN ColumnText n
ON n.TextID = m.Val
)p
PIVOT(MAX(Text) FOR Cat IN ([L1],[L2],...,[L10]))q
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
Edited by - visakh16 on 11/20/2012 02:18:56 |
 |
|
| |
Topic  |
|
|
|