Inferno Inferno - 1 year ago 67
SQL Question

I need a SQL Query as follows

This is my following question please have a look in the code snippet.

Hi, I have two tables Table A, Table B as follows:

**Table A** **Table B**
Reg Id (ex: 217) Session Name (ex: Section A, Section B)
First Choice
Second Choice
Third Choice
Reg Id

I need the result as follows:

**Reg id** **Section A** **Section B** **Section C**
217 First Choice First Choice First Choice
Second Choice Second Choice Second Choice
Third Choice Third Choice Third Choice

Answer Source

Here is how I wold do it. Join for each column.

       BSecA.FirstChoice || ', ' || BSecA.SecondChoice || ', ' || BSecA.ThirdChoice as SectionA
       BSecB.FirstChoice || ', ' || BSecB.SecondChoice || ', ' || BSecB.ThirdChoice as SectionB 
       BSecC.FirstChoice || ', ' || BSecC.SecondChoice || ', ' || BSecC.ThirdChoice as SectionC 
LEFT JOIN TableB AS BsecA ON A.RegID = BsecA.RegID AND BsecA.SectionName = "Section A"
LEFT JOIN TableB AS BsecB ON A.RegID = BsecB.RegID AND BsecB.SectionName = "Section B"
LEFT JOIN TableB AS BsecC ON A.RegID = BsecC.RegID AND BsecC.SectionName = "Section C"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download