2021年4月10日星期六

Query on Joins - Each column is a key in other tables. Need to join and fetch the corresponding column value

I am having a problem framing a query with joins. Below my tables structure and data.

Tabl1(Contains ID's and Code's. The description for these codes is present in tabl3.)

ID1 ID2 CD1 CD2
1 101 MAC BMW
1 102 XPS AUD

Tabl2(Contains Customer names)

tab_id1 tab_id2 Cust_Name
1 101 Cust Name 1
1 102 Cust Name 2

Tabl3(Contains Description of codes present in tabl1.)

tab_id1 Code Description
1 MAC Apple laptop
1 XPS Dell laptop
1 BMW BMW Auto
1 AUD Audi Auto

Tabl1 holds the codes in the columns cd1,cd2. For these codes the description is defined in tabl3. so I need to join tabl3 and get the description values and display it. so I need to write a query that should have the following output.

id1, id2, cust_name, cd1 ||' - '|| description for cd1, cd2 || ' - ' ||description for cd2.  

I tried the below query but I am getting multiple rows(4 rows whereas the output should have only 2 rows).

select      t1.id1,      t1.id2,      t2.cust_name,      t1.cd1,      t3.description,      t1.cd2,      t3.description  from      tabl1 t1,      tabl2 t2,      tabl3 t3  where      t1.id2 = t2.tab_id2      AND t1.id1 = t3.tab_id1      AND t1.id1 = t2.tab_id1      AND (          t3.code = t1.cd1          or t3.code = t1.cd2      );  

Please provide me with any pointers. Thank You.

https://stackoverflow.com/questions/67040875/query-on-joins-each-column-is-a-key-in-other-tables-need-to-join-and-fetch-th April 11, 2021 at 10:16AM

没有评论:

发表评论