Skip to content
Advertisement

How to join multiple tables and printing the result for same id which are populating more than one in a single row

The problem facing in joining the tables :

Table ques_ans has ref_ques_id column that is being populated from ques_ref table. So, If ref_ques_id = 3 and ref_ans_id = 1 then it should display ‘Yes’ i.e populating from ans_ref table. Likewise for ref_ques_id = 2 then it should display ‘No’ and same for ref_ques_id = 4.

My Attempt :

In my attempt, I am getting 3 rows but ideally expected output should be like attached in the screenshot.

Expected Output

Columne e_id : Coming from details_1 table

Column Changes_exist: Validation in the ques_ans table ref_ques_id column and based on the ref_ans_id printing Yes or no.

Column E_clubbed: Validation in the ques_ans table ref_ques_id column and based on the ref_ans_id printing Yes or no.

Column E_Impacted: Validation in the ques_ans table ref_ques_id column and based on the ref_ans_id printing Yes or no.

The output should be like the attached screenshot but I got stuck that how this can be printed in a single row

Tool: SQL Developer Version: 20.4

Advertisement

Answer

Code you wrote almost does it; there’s another join missing (with ques_ref) so that you’d extract code name, but that still doesn’t “fix” it because – as far as I can tell – there’s no way to do it “dynamically” within SQL. What we usually do is to pivot data. One option is to use an aggregate function (max in this example) along with case expression, and to literally name every column (using code name you’ve previously extracted, but just to be of some help while writing column’s alias).

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement