1

I have table as follow in my POSTGRES database

Table name:userDetails

column 1: userId (Bigint)

column 2: questionId1 (int) Foreign key:: userQuestions(id)

column 3: answer1 (String)

column 4: questionId2 (int) Foreign key:: userQuestions(id)    |

column 5: answer2 (String) 

Table name: userQuestions

column 1: id(bigint)

column 2: question(String)

and i want to select output as follow on the basis of userId,

column 1: userId (Bigint)

column 2: questionId's (int [])(questionId1 and questionId2)

column 3: questions (String [])( array of questions from table userQuestions against the the questionId1 and questionId2 )

column 4: answer(String []) (answer1 and answer2 from userDetails table in array of String)

please help me to write sql query.

SAMPLE DATA

Tablename :: userDetails

|userId         |questionId1        |answer1        |questionId2        |answer2

  abc                 1               "hp"                2               "tommy"

Tablename :: userQuestions

id          question 

1           "What is brand name of your laptop?"

2           "What is name of your pet?"

Expected OUTPUT::

userId          questionIds        answers                       questions

 abcd              [1,2]        ["hp","tommy"]              ["What is brand 
                                                              name of your 
                                                              laptop?",
                                                            "What is name of 
                                                             your pet?"]
4
  • 1
    Sample data and desired results would really help. Commented Aug 1, 2018 at 11:02
  • @ Gordon i have edited my question and have added some sample data and expected output, can you help me in writing sql query accordingly Commented Aug 1, 2018 at 12:06
  • . . Something is wrong with your data structure. UserQuestions doesn't seem to have a connection to Users. I'm expecting a junction table, with one row per user and per question. Commented Aug 1, 2018 at 12:13
  • Yes their is relation @Gordon, the columns questionId1 and questionId2 from table userDetails has foreign key on column "id" of table "userQuestions" which i have mentioned above Commented Aug 2, 2018 at 4:25

2 Answers 2

1

I would put it differently

select d.userid, d.questionid1 || ',' || d.questionid2, d.answer1 || ' ' || d.answer2, 
        string_agg(q.question,',')
from userDetails d
join userQuestions q on q.id = d.questionid1 or q.id = d.questionid2
GROUP BY d.userid
Sign up to request clarification or add additional context in comments.

1 Comment

This one worked @Tito Thank you ,it only needs 'GROUP BY d.userid' at the end
1

I think the only way is to insert the output in a temporary table, that way you can insert questionId1 with it's question string and answer1, then insert questionId2 with it's question string and answer2 to the same columns, and then show the results.

Edit: As you requested here is an example that works in SQL Server (I don't know about POSTGRES, never worked on it)

Select t1.userId, t1.questionId1 as questionId, (Select t2.question from userQuestions t2 where t2.id = t1.questionId1) as questions, answer1 as answer into #TempTable from userDetails
UNION ALL Select t1.userId, t1.questionId2 as questionId, (Select t2.question from userQuestions t2 where t2.id = t1.questionId2) as questions, answer2 as answer into #TempTable from userDetails
Select * from #TempTable

Don't forget before re-executing the code you'll have to delete the temporary table using Drop table #TempTable

I have tried a simillar code on SQL Server 2016 and it's working just fine.

2 Comments

@ Tonner Maan as i am new to sql can u please share the sql query for it
@BZT there you go an example in SQL Server, not sure if postgresql uses the same queries but you can edit as you need to achieve your goal, hope it helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.