Search code examples
sqlsql-serverpivotunpivot

Joining 3 SQL Querys with Pivot/Unpivot in them


I have a tables named MachineSweepQuestions , MachineSweepAnswers , MachineSweetComments, I need to join all 3 of these tables using what i think are Pivots as displayed in my code so they can share an outcome via a few parameters. Below are pictures of the code with a image of the returned results and a Idea of what my end goal should but , all these tables share the column name AreaID and LastChecked ( These will be the parameters passed.

Question Query:

Select AreaID, Questions From (Select AreaID, Question1,
Question2,
Question3,
Question4,
Question5,
Question6,
Question7,
Question8,
Question9,
Question10,
Question11,
Question12,
Question13,
Question14
From MachineSweepQuestions) x
UNPIVOT
(
Questions
For QuestionID in (Question1, Question2, Question3, Question4, Question5, Question6,Question7,Question8,Question9,Question10,Question11,Question12,Question13,Question14)
) unpiv Where AreaID='8'

Questions

Answers Query:

 Select Answer,LastChecked From (select AreaID,Answer1,
 Answer2,
 Answer3,
 Answer4,
 Answer5,
 Answer6,
 Answer7,
 Answer8,
 Answer9,
 Answer10,
 Answer11,
 Answer12,
 Answer13,
 Answer14,
 LastChecked
 From MachineSweepAnswers) x 
 UNPIVOT
 (
 Answer
 For AnswerID
in(Answer1,Answer2,Answer3,Answer4,Answer5,Answer6,Answer7,Answer8,Answer9,Answer10,Answer11,Answer12,Answer13,Answer14)
 ) unpiv Where AreaID = '8' AND LastChecked = '2014-04-01'

enter image description here

The Hopeful Final Outcome:

FinalOutcome

so my question is , Does anyone know how to Join these Pivots with my comments query to achieve the outcome of below as I've tried many ways and searched for answers in the hopes of guidance yet have come to a dead end, any help is welcome. ( My paint skills make De Vinci cry! ) ...

EDIT!:! I missed comments off , the SQL query is Select CommentsBox,Lastchecked From MachineSweepComments Where AreaID='8' And LastChecked='2014-04-01'


Solution

  • Have a try at this :

    Select * from (Select AreaID, Questions From (Select AreaID, Question1,
    Question2,
    Question3,
    Question4,
    Question5, 
    Question6,
    Question7, 
    Question8, 
    Question9, 
    Question10,
    Question11,
    Question12,
    Question13,
    Question14 From
    MachineSweepQuestions) x UNPIVOT ( Questions For QuestionID in
       (Question1,
        Question2, 
        Question3, 
        Question4, 
        Question5,
        Question6,
        Question7,
        Question8,
        Question9,
        Question10,
        Question11,
        Question12,
        Question13,
        Question14)
       ) unpiv Where AreaID=@AreaId) AS tbl1 LEFT OUTER JOIN 
              (Select Answer,
                      LastChecked, AreaId From (select AreaID,
                                               Answer1,
                                               Answer2,
                                               Answer3,
                                               Answer4,
                                               Answer5,
                                               Answer6,
                                               Answer7,
                                               Answer8,
                                               Answer9,
                                               Answer10,
                                               Answer11,
                                               Answer12,
                                               Answer13,
                                               Answer14,
                                               LastChecked
                                                     From MachineSweepAnswers) x 
                                                 UNPIVOT
                                                     (
                                                     Answer
                                                     For AnswerID
                                                     in(Answer1,
                                                     Answer2,
                                                     Answer3,
                                                     Answer4,
                                                     Answer5,
                                                     Answer6,
                                                     Answer7,
                                                     Answer8,
                                                     Answer9,
                                                     Answer10,
                                                     Answer11,
                                                     Answer12,
                                                     Answer13,
                                                     Answer14)
                                                     ) unpiv Where AreaID = @AreaId 
                                                     AND LastChecked = '2014-04-01') 
                            AS tbl2 ON tbl1.AreaId=tbl2.AreaId
                  LEFT OUTER JOIN (Select CommentsBox,
                                     Lastchecked,
                                     AreaId From MachineSweepComments 
                                                 Where AreaID=@AreaId 
                                                 OR AreaId IS NULL
                                                 And LastChecked='2014-04-01') 
                             AS tbl3 ON tbl1.AreaId=tbl3.AreaId
    

    If this doesn't work then feel free to talk to me