Reputation: 13
I am using DBForge Query Builder
I have one table with a number of columns which contain abbreviations. Another table contains the descriptions for the abbreviations.
What is a query to replace the abbreviations with their descriptions?
I tried a number of joins without any success.
T1
ID Date Type Cat Sub Cat
1 01/09/18 E F L
2 05/09/18 Cc F D
3 06/09/18 Cc C Dr
4 08/09/18 Cc C Sh
5 08/09/18 E C Sh
T2
Code Des
E Eft Payment
Cc Credit Card
F Food
C Clothes
B Breakfast
L Lunch
D Dinner
Sh Shirt
Dr Dress
Desired Output
ID Date Type D Cat D Sub Cat D
1 01/09/18 Eft Payment Food Lunch
2 05/09/18 Credit Card Food Dinner
3 06/09/18 Credit Card Clothes Dress
4 08/09/18 Credit Card Clothes Shirt
5 08/09/18 Eft Payment Clothes Shirt
Upvotes: 0
Views: 192
Reputation: 121922
We’d recommend the following solution:
SELECT t_type_code.id,
t_type_code.Date,
t_type_code.Des AS Type_D,
t_cat_code.Des AS Cat_D,
t_sub_code.Des AS Sub_Cat_D
FROM
(SELECT * FROM t1 JOIN t2 ON t1.Type=t2.Code) AS t_type_code
JOIN
(SELECT * FROM t1 JOIN t2 ON t1.cat=t2.Code) AS t_cat_code
ON t_type_code.id=t_cat_code.id
JOIN
(SELECT * FROM t1 JOIN t2 ON t1.Sub_Cat=t2.Code) AS t_sub_code
ON t_cat_code.id=t_sub_code.id
Upvotes: 0
Reputation: 2592
You need to use multiple instances of the T2 table:
select t1.id, t1.date, typ.des as type_d,ca.des as cat_d,subca.des as sub_cat_d from T1 t1
inner join T2 typ on t1.type=typ.code
inner join T2 ca on t1.cat = ca.code
inner join T2 subca on t1.subcat = subca.code;
Try this...
Upvotes: 0
Reputation: 26
I will assume the columns of T1 are (ID, Date, Type, Cat, SubCat
)
SELECT T1.ID, T1.Date, T2Type.Des, T2Cat.Des, T2SubCat.Des
FROM T1 INNER JOIN T2 T2Type ON T1.Type = T2Type.Code
INNER JOIN T2 T2Cat ON T1.Cat = T2Cat.Code
INNER JOIN T2 T2SubCat ON T1.SubCat = T2SubCat.Code
Upvotes: 0
Reputation: 37473
Try below using multiple join by T2 with different alias
select
a.ID, a.date,
b.Des as TypeD,
c.Des as CatD,
d.Des as SubCatD
from T1 a inner join T2 b on a.type=b.code
inner join T2 c on a.Cat=c.code
inner join T2 d in a.SubCat=d.code
Upvotes: 0