Need help sorting out my JOIN statements for multiple sub queries












0















I will be up front and say I'm aware this is a messy query. The database is not managed by me so some of the relations are long and complex. It's a hosted EMR so I have very minimal access as to what SQL commands I can use. For instance I cannot have anything before the first SELECT statement. Then there is the fact that SQL is just something I know a tiny bit about so these reports get pushed on me and I'm not SQL expert.



That said I'm trying to pull in 3 separate queries for answer to questions we have on a form. I'm not sure how to bind these with the correct join and I have no doubt there is a better way to do it.



Here is my existing Query:



Select Distinct C.client_id,
C.first_name,
C.last_name,
CV.rev_timein,
Q1.answer,
Q2.answer,
Q3.answer

From Clients C,

(
Select C2.client_id as client_id,
Answer.answer as answer
From Clients C2
Inner Join ClientVisit On C2.client_id = ClientVisit.client_id
Left Join SavedVisitAnswer On SavedVisitAnswer.clientvisit_id =
ClientVisit.clientvisit_id
Left Join Question On Question.question_id = SavedVisitAnswer.question_id
Left Join Category On Question.category_id = Category.category_id
Left Join FormVersion On Category.form_ver_id = FormVersion.form_ver_id
Left Join Forms On Forms.form_id = FormVersion.form_id
Inner Join Answer On SavedVisitAnswer.answer_id = Answer.answer_id
Where SavedVisitAnswer.question_id = '518722' And Forms.form_id = '336' And
FormVersion.is_active = '1' And ClientVisit.rev_timein >= @param1 And
ClientVisit.rev_timein < DateAdd(d, 1, @param2)
GROUP BY C2.client_id, Answer.answer
) Q1,

(
Select C3.client_id as client_id,
Answer.answer as answer
From Clients C3
Inner Join ClientVisit On C3.client_id = ClientVisit.client_id
Left Join SavedVisitAnswer On SavedVisitAnswer.clientvisit_id =
ClientVisit.clientvisit_id
Left Join Question On Question.question_id = SavedVisitAnswer.question_id
Left Join Category On Question.category_id = Category.category_id
Left Join FormVersion On Category.form_ver_id = FormVersion.form_ver_id
Left Join Forms On Forms.form_id = FormVersion.form_id
Inner Join Answer On SavedVisitAnswer.answer_id = Answer.answer_id
Where SavedVisitAnswer.question_id = '518727' And Forms.form_id = '336' And
FormVersion.is_active = '1' And ClientVisit.rev_timein >= @param1 And
ClientVisit.rev_timein < DateAdd(d, 1, @param2)
GROUP BY C3.client_id, Answer.answer
) Q2,

(
Select C4.client_id as client_id,
Answer.answer as answer
From Clients C4
Inner Join ClientVisit On C4.client_id = ClientVisit.client_id
Left Join SavedVisitAnswer On SavedVisitAnswer.clientvisit_id =
ClientVisit.clientvisit_id
Left Join Question On Question.question_id = SavedVisitAnswer.question_id
Left Join Category On Question.category_id = Category.category_id
Left Join FormVersion On Category.form_ver_id = FormVersion.form_ver_id
Left Join Forms On Forms.form_id = FormVersion.form_id
Inner Join Answer On SavedVisitAnswer.answer_id = Answer.answer_id
Where SavedVisitAnswer.question_id = '518728' And Forms.form_id = '336' And
FormVersion.is_active = '1' And ClientVisit.rev_timein >= @param1 And
ClientVisit.rev_timein < DateAdd(d, 1, @param2)
GROUP BY C4.client_id, Answer.answer
) Q3


Inner Join ClientVisit CV On C.client_id = ClientVisit.client_id
Left Join SavedVisitAnswer On SavedVisitAnswer.clientvisit_id =
ClientVisit.clientvisit_id
Left Join Question On Question.question_id = SavedVisitAnswer.question_id
Left Join Category On Question.category_id = Category.category_id
Left Join FormVersion On Category.form_ver_id = FormVersion.form_ver_id
Left Join Forms On Forms.form_id = FormVersion.form_id
Inner Join Answer On SavedVisitAnswer.answer_id = Answer.answer_id

Where Forms.form_id = '336' And
FormVersion.is_active = '1' And CV.rev_timein >= @param1 And
CV.rev_timein < DateAdd(d, 1, @param2)
Order By C.last_name


The goal is to have the output for each client in the date range formated as such.



Client_ID, First Name, Last Name, Date-Time, Answer1, Answer2, Answer3










share|improve this question



























    0















    I will be up front and say I'm aware this is a messy query. The database is not managed by me so some of the relations are long and complex. It's a hosted EMR so I have very minimal access as to what SQL commands I can use. For instance I cannot have anything before the first SELECT statement. Then there is the fact that SQL is just something I know a tiny bit about so these reports get pushed on me and I'm not SQL expert.



    That said I'm trying to pull in 3 separate queries for answer to questions we have on a form. I'm not sure how to bind these with the correct join and I have no doubt there is a better way to do it.



    Here is my existing Query:



    Select Distinct C.client_id,
    C.first_name,
    C.last_name,
    CV.rev_timein,
    Q1.answer,
    Q2.answer,
    Q3.answer

    From Clients C,

    (
    Select C2.client_id as client_id,
    Answer.answer as answer
    From Clients C2
    Inner Join ClientVisit On C2.client_id = ClientVisit.client_id
    Left Join SavedVisitAnswer On SavedVisitAnswer.clientvisit_id =
    ClientVisit.clientvisit_id
    Left Join Question On Question.question_id = SavedVisitAnswer.question_id
    Left Join Category On Question.category_id = Category.category_id
    Left Join FormVersion On Category.form_ver_id = FormVersion.form_ver_id
    Left Join Forms On Forms.form_id = FormVersion.form_id
    Inner Join Answer On SavedVisitAnswer.answer_id = Answer.answer_id
    Where SavedVisitAnswer.question_id = '518722' And Forms.form_id = '336' And
    FormVersion.is_active = '1' And ClientVisit.rev_timein >= @param1 And
    ClientVisit.rev_timein < DateAdd(d, 1, @param2)
    GROUP BY C2.client_id, Answer.answer
    ) Q1,

    (
    Select C3.client_id as client_id,
    Answer.answer as answer
    From Clients C3
    Inner Join ClientVisit On C3.client_id = ClientVisit.client_id
    Left Join SavedVisitAnswer On SavedVisitAnswer.clientvisit_id =
    ClientVisit.clientvisit_id
    Left Join Question On Question.question_id = SavedVisitAnswer.question_id
    Left Join Category On Question.category_id = Category.category_id
    Left Join FormVersion On Category.form_ver_id = FormVersion.form_ver_id
    Left Join Forms On Forms.form_id = FormVersion.form_id
    Inner Join Answer On SavedVisitAnswer.answer_id = Answer.answer_id
    Where SavedVisitAnswer.question_id = '518727' And Forms.form_id = '336' And
    FormVersion.is_active = '1' And ClientVisit.rev_timein >= @param1 And
    ClientVisit.rev_timein < DateAdd(d, 1, @param2)
    GROUP BY C3.client_id, Answer.answer
    ) Q2,

    (
    Select C4.client_id as client_id,
    Answer.answer as answer
    From Clients C4
    Inner Join ClientVisit On C4.client_id = ClientVisit.client_id
    Left Join SavedVisitAnswer On SavedVisitAnswer.clientvisit_id =
    ClientVisit.clientvisit_id
    Left Join Question On Question.question_id = SavedVisitAnswer.question_id
    Left Join Category On Question.category_id = Category.category_id
    Left Join FormVersion On Category.form_ver_id = FormVersion.form_ver_id
    Left Join Forms On Forms.form_id = FormVersion.form_id
    Inner Join Answer On SavedVisitAnswer.answer_id = Answer.answer_id
    Where SavedVisitAnswer.question_id = '518728' And Forms.form_id = '336' And
    FormVersion.is_active = '1' And ClientVisit.rev_timein >= @param1 And
    ClientVisit.rev_timein < DateAdd(d, 1, @param2)
    GROUP BY C4.client_id, Answer.answer
    ) Q3


    Inner Join ClientVisit CV On C.client_id = ClientVisit.client_id
    Left Join SavedVisitAnswer On SavedVisitAnswer.clientvisit_id =
    ClientVisit.clientvisit_id
    Left Join Question On Question.question_id = SavedVisitAnswer.question_id
    Left Join Category On Question.category_id = Category.category_id
    Left Join FormVersion On Category.form_ver_id = FormVersion.form_ver_id
    Left Join Forms On Forms.form_id = FormVersion.form_id
    Inner Join Answer On SavedVisitAnswer.answer_id = Answer.answer_id

    Where Forms.form_id = '336' And
    FormVersion.is_active = '1' And CV.rev_timein >= @param1 And
    CV.rev_timein < DateAdd(d, 1, @param2)
    Order By C.last_name


    The goal is to have the output for each client in the date range formated as such.



    Client_ID, First Name, Last Name, Date-Time, Answer1, Answer2, Answer3










    share|improve this question

























      0












      0








      0








      I will be up front and say I'm aware this is a messy query. The database is not managed by me so some of the relations are long and complex. It's a hosted EMR so I have very minimal access as to what SQL commands I can use. For instance I cannot have anything before the first SELECT statement. Then there is the fact that SQL is just something I know a tiny bit about so these reports get pushed on me and I'm not SQL expert.



      That said I'm trying to pull in 3 separate queries for answer to questions we have on a form. I'm not sure how to bind these with the correct join and I have no doubt there is a better way to do it.



      Here is my existing Query:



      Select Distinct C.client_id,
      C.first_name,
      C.last_name,
      CV.rev_timein,
      Q1.answer,
      Q2.answer,
      Q3.answer

      From Clients C,

      (
      Select C2.client_id as client_id,
      Answer.answer as answer
      From Clients C2
      Inner Join ClientVisit On C2.client_id = ClientVisit.client_id
      Left Join SavedVisitAnswer On SavedVisitAnswer.clientvisit_id =
      ClientVisit.clientvisit_id
      Left Join Question On Question.question_id = SavedVisitAnswer.question_id
      Left Join Category On Question.category_id = Category.category_id
      Left Join FormVersion On Category.form_ver_id = FormVersion.form_ver_id
      Left Join Forms On Forms.form_id = FormVersion.form_id
      Inner Join Answer On SavedVisitAnswer.answer_id = Answer.answer_id
      Where SavedVisitAnswer.question_id = '518722' And Forms.form_id = '336' And
      FormVersion.is_active = '1' And ClientVisit.rev_timein >= @param1 And
      ClientVisit.rev_timein < DateAdd(d, 1, @param2)
      GROUP BY C2.client_id, Answer.answer
      ) Q1,

      (
      Select C3.client_id as client_id,
      Answer.answer as answer
      From Clients C3
      Inner Join ClientVisit On C3.client_id = ClientVisit.client_id
      Left Join SavedVisitAnswer On SavedVisitAnswer.clientvisit_id =
      ClientVisit.clientvisit_id
      Left Join Question On Question.question_id = SavedVisitAnswer.question_id
      Left Join Category On Question.category_id = Category.category_id
      Left Join FormVersion On Category.form_ver_id = FormVersion.form_ver_id
      Left Join Forms On Forms.form_id = FormVersion.form_id
      Inner Join Answer On SavedVisitAnswer.answer_id = Answer.answer_id
      Where SavedVisitAnswer.question_id = '518727' And Forms.form_id = '336' And
      FormVersion.is_active = '1' And ClientVisit.rev_timein >= @param1 And
      ClientVisit.rev_timein < DateAdd(d, 1, @param2)
      GROUP BY C3.client_id, Answer.answer
      ) Q2,

      (
      Select C4.client_id as client_id,
      Answer.answer as answer
      From Clients C4
      Inner Join ClientVisit On C4.client_id = ClientVisit.client_id
      Left Join SavedVisitAnswer On SavedVisitAnswer.clientvisit_id =
      ClientVisit.clientvisit_id
      Left Join Question On Question.question_id = SavedVisitAnswer.question_id
      Left Join Category On Question.category_id = Category.category_id
      Left Join FormVersion On Category.form_ver_id = FormVersion.form_ver_id
      Left Join Forms On Forms.form_id = FormVersion.form_id
      Inner Join Answer On SavedVisitAnswer.answer_id = Answer.answer_id
      Where SavedVisitAnswer.question_id = '518728' And Forms.form_id = '336' And
      FormVersion.is_active = '1' And ClientVisit.rev_timein >= @param1 And
      ClientVisit.rev_timein < DateAdd(d, 1, @param2)
      GROUP BY C4.client_id, Answer.answer
      ) Q3


      Inner Join ClientVisit CV On C.client_id = ClientVisit.client_id
      Left Join SavedVisitAnswer On SavedVisitAnswer.clientvisit_id =
      ClientVisit.clientvisit_id
      Left Join Question On Question.question_id = SavedVisitAnswer.question_id
      Left Join Category On Question.category_id = Category.category_id
      Left Join FormVersion On Category.form_ver_id = FormVersion.form_ver_id
      Left Join Forms On Forms.form_id = FormVersion.form_id
      Inner Join Answer On SavedVisitAnswer.answer_id = Answer.answer_id

      Where Forms.form_id = '336' And
      FormVersion.is_active = '1' And CV.rev_timein >= @param1 And
      CV.rev_timein < DateAdd(d, 1, @param2)
      Order By C.last_name


      The goal is to have the output for each client in the date range formated as such.



      Client_ID, First Name, Last Name, Date-Time, Answer1, Answer2, Answer3










      share|improve this question














      I will be up front and say I'm aware this is a messy query. The database is not managed by me so some of the relations are long and complex. It's a hosted EMR so I have very minimal access as to what SQL commands I can use. For instance I cannot have anything before the first SELECT statement. Then there is the fact that SQL is just something I know a tiny bit about so these reports get pushed on me and I'm not SQL expert.



      That said I'm trying to pull in 3 separate queries for answer to questions we have on a form. I'm not sure how to bind these with the correct join and I have no doubt there is a better way to do it.



      Here is my existing Query:



      Select Distinct C.client_id,
      C.first_name,
      C.last_name,
      CV.rev_timein,
      Q1.answer,
      Q2.answer,
      Q3.answer

      From Clients C,

      (
      Select C2.client_id as client_id,
      Answer.answer as answer
      From Clients C2
      Inner Join ClientVisit On C2.client_id = ClientVisit.client_id
      Left Join SavedVisitAnswer On SavedVisitAnswer.clientvisit_id =
      ClientVisit.clientvisit_id
      Left Join Question On Question.question_id = SavedVisitAnswer.question_id
      Left Join Category On Question.category_id = Category.category_id
      Left Join FormVersion On Category.form_ver_id = FormVersion.form_ver_id
      Left Join Forms On Forms.form_id = FormVersion.form_id
      Inner Join Answer On SavedVisitAnswer.answer_id = Answer.answer_id
      Where SavedVisitAnswer.question_id = '518722' And Forms.form_id = '336' And
      FormVersion.is_active = '1' And ClientVisit.rev_timein >= @param1 And
      ClientVisit.rev_timein < DateAdd(d, 1, @param2)
      GROUP BY C2.client_id, Answer.answer
      ) Q1,

      (
      Select C3.client_id as client_id,
      Answer.answer as answer
      From Clients C3
      Inner Join ClientVisit On C3.client_id = ClientVisit.client_id
      Left Join SavedVisitAnswer On SavedVisitAnswer.clientvisit_id =
      ClientVisit.clientvisit_id
      Left Join Question On Question.question_id = SavedVisitAnswer.question_id
      Left Join Category On Question.category_id = Category.category_id
      Left Join FormVersion On Category.form_ver_id = FormVersion.form_ver_id
      Left Join Forms On Forms.form_id = FormVersion.form_id
      Inner Join Answer On SavedVisitAnswer.answer_id = Answer.answer_id
      Where SavedVisitAnswer.question_id = '518727' And Forms.form_id = '336' And
      FormVersion.is_active = '1' And ClientVisit.rev_timein >= @param1 And
      ClientVisit.rev_timein < DateAdd(d, 1, @param2)
      GROUP BY C3.client_id, Answer.answer
      ) Q2,

      (
      Select C4.client_id as client_id,
      Answer.answer as answer
      From Clients C4
      Inner Join ClientVisit On C4.client_id = ClientVisit.client_id
      Left Join SavedVisitAnswer On SavedVisitAnswer.clientvisit_id =
      ClientVisit.clientvisit_id
      Left Join Question On Question.question_id = SavedVisitAnswer.question_id
      Left Join Category On Question.category_id = Category.category_id
      Left Join FormVersion On Category.form_ver_id = FormVersion.form_ver_id
      Left Join Forms On Forms.form_id = FormVersion.form_id
      Inner Join Answer On SavedVisitAnswer.answer_id = Answer.answer_id
      Where SavedVisitAnswer.question_id = '518728' And Forms.form_id = '336' And
      FormVersion.is_active = '1' And ClientVisit.rev_timein >= @param1 And
      ClientVisit.rev_timein < DateAdd(d, 1, @param2)
      GROUP BY C4.client_id, Answer.answer
      ) Q3


      Inner Join ClientVisit CV On C.client_id = ClientVisit.client_id
      Left Join SavedVisitAnswer On SavedVisitAnswer.clientvisit_id =
      ClientVisit.clientvisit_id
      Left Join Question On Question.question_id = SavedVisitAnswer.question_id
      Left Join Category On Question.category_id = Category.category_id
      Left Join FormVersion On Category.form_ver_id = FormVersion.form_ver_id
      Left Join Forms On Forms.form_id = FormVersion.form_id
      Inner Join Answer On SavedVisitAnswer.answer_id = Answer.answer_id

      Where Forms.form_id = '336' And
      FormVersion.is_active = '1' And CV.rev_timein >= @param1 And
      CV.rev_timein < DateAdd(d, 1, @param2)
      Order By C.last_name


      The goal is to have the output for each client in the date range formated as such.



      Client_ID, First Name, Last Name, Date-Time, Answer1, Answer2, Answer3







      sql sql-server tsql






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 14:42









      BryanBryan

      93




      93
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You don't need to do three sub query for what you need. Also you don't need to filter in the last where clause because you already did it in the sub-queries. I tried to create simple solution for you. If you search three single answer for three specific question it should work for you:



          DECLARE @DateFrom    DATE
          DECLARE @DateTo DATE
          DECLARE @QuestionId1 INT
          DECLARE @QuestionId2 INT
          DECLARE @QuestionId3 INT
          DECLARE @FormId INT

          SET @DateFrom ='2018-11-01'
          SET @DateTo ='2018-11-30'
          SET @FormId =336
          SET @QuestionId1 =518722
          SET @QuestionId2 =518727
          SET @QuestionId3 =518728

          SELECT
          C.client_id
          ,C.first_name
          ,C.last_name
          ,ClientVisit.rev_timein
          ,MAX(CASE WHEN SavedVisitAnswer.question_id = @QuestionId1 THEN Answer.Answer ELSE '' END) Answer1
          ,MAX(CASE WHEN SavedVisitAnswer.question_id = @QuestionId2 THEN Answer.Answer ELSE '' END) Answer2
          ,MAX(CASE WHEN SavedVisitAnswer.question_id = @QuestionId3 THEN Answer.Answer ELSE '' END) Answer3
          FROM Clients C
          INNER JOIN ClientVisit ON C.client_id = ClientVisit.client_id
          INNER JOIN SavedVisitAnswer ON SavedVisitAnswer.clientvisit_id = ClientVisit.clientvisit_id
          INNER JOIN Question ON Question.question_id = SavedVisitAnswer.question_id
          INNER JOIN Category ON Question.category_id = Category.category_id
          INNER JOIN FormVersion ON Category.form_ver_id = FormVersion.form_ver_id
          INNER JOIN Forms ON Forms.form_id = FormVersion.form_id
          INNER JOIN Answer ON SavedVisitAnswer.answer_id = Answer.answer_id
          WHERE Forms.form_id = @FormId
          AND FormVersion.is_active = '1'
          AND ClientVisit.rev_timein >= @DateFrom
          AND ClientVisit.rev_timein < DATEADD(DAY, 1, @DateTo)
          GROUP BY
          C.client_id
          ,C.first_name
          ,C.last_name
          ,ClientVisit.rev_timein


          EDIT I removed all variables. including your @param1 and @param2. If you want to use them put them back and execute it:



          SELECT 
          C.client_id
          ,C.first_name
          ,C.last_name
          ,ClientVisit.rev_timein
          ,MAX(CASE WHEN SavedVisitAnswer.question_id = 518722 THEN Answer.Answer ELSE '' END) Answer1
          ,MAX(CASE WHEN SavedVisitAnswer.question_id = 518727 THEN Answer.Answer ELSE '' END) Answer2
          ,MAX(CASE WHEN SavedVisitAnswer.question_id = 518728 THEN Answer.Answer ELSE '' END) Answer3
          FROM Clients C
          INNER JOIN ClientVisit ON C.client_id = ClientVisit.client_id
          INNER JOIN SavedVisitAnswer ON SavedVisitAnswer.clientvisit_id = ClientVisit.clientvisit_id
          INNER JOIN Question ON Question.question_id = SavedVisitAnswer.question_id
          INNER JOIN Category ON Question.category_id = Category.category_id
          INNER JOIN FormVersion ON Category.form_ver_id = FormVersion.form_ver_id
          INNER JOIN Forms ON Forms.form_id = FormVersion.form_id
          INNER JOIN Answer ON SavedVisitAnswer.answer_id = Answer.answer_id
          WHERE Forms.form_id = 336
          AND FormVersion.is_active = '1'
          AND ClientVisit.rev_timein >= '2018-11-01' /*@param1?*/
          AND ClientVisit.rev_timein < DATEADD(DAY, 1, '2018-11-30' /*param2?*/)
          GROUP BY
          C.client_id
          ,C.first_name
          ,C.last_name
          ,ClientVisit.rev_timein





          share|improve this answer


























          • Just to be clear, your DECLARE and SET statements are just for cleanliness? I ask because I cannot use them as the interface I have access to requires SELECT to be the very first thing, I cannot even have white space before it. I'm going to give this a try though, I just wanted to make sure.

            – Bryan
            Nov 21 '18 at 15:36











          • @Bryan , I saw param1 and param2 in your script, because of that I thought you can use variables. Anyway, I have added another query with no variable including param1 and param2. Change the dates and and execute it.

            – Zeki Gumus
            Nov 21 '18 at 16:06











          • Ok, that's what I thought. param1, param2 and param3 are generated by the web front-end so I have access to a total of 3 variables. As you can see they make it real easy to do reports(sarcasm). I am getting an error with query and it's possible I may have to contact the EMR provider as I think it's a bug. It's telling me "Ambiguous column name 'Answer'. " and Answer isn't a column it's a table. Unless you have some insight I'm going to assume this is a bug in there interpreter.

            – Bryan
            Nov 21 '18 at 16:18













          • @Bryan , So, you have Answer field in more than one table that you use in join. I have updated Answer as Answer.Answer. Try again.

            – Zeki Gumus
            Nov 21 '18 at 16:25













          • Thank you very much. This is much simpler and makes a lot of sense. I hope that as I work with this more it becomes easier and my queries less messy. I was actually hired on as a 'Programmer' and it turns out they really just wanted an SQL Report Writer.

            – Bryan
            Nov 21 '18 at 18:02











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414506%2fneed-help-sorting-out-my-join-statements-for-multiple-sub-queries%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You don't need to do three sub query for what you need. Also you don't need to filter in the last where clause because you already did it in the sub-queries. I tried to create simple solution for you. If you search three single answer for three specific question it should work for you:



          DECLARE @DateFrom    DATE
          DECLARE @DateTo DATE
          DECLARE @QuestionId1 INT
          DECLARE @QuestionId2 INT
          DECLARE @QuestionId3 INT
          DECLARE @FormId INT

          SET @DateFrom ='2018-11-01'
          SET @DateTo ='2018-11-30'
          SET @FormId =336
          SET @QuestionId1 =518722
          SET @QuestionId2 =518727
          SET @QuestionId3 =518728

          SELECT
          C.client_id
          ,C.first_name
          ,C.last_name
          ,ClientVisit.rev_timein
          ,MAX(CASE WHEN SavedVisitAnswer.question_id = @QuestionId1 THEN Answer.Answer ELSE '' END) Answer1
          ,MAX(CASE WHEN SavedVisitAnswer.question_id = @QuestionId2 THEN Answer.Answer ELSE '' END) Answer2
          ,MAX(CASE WHEN SavedVisitAnswer.question_id = @QuestionId3 THEN Answer.Answer ELSE '' END) Answer3
          FROM Clients C
          INNER JOIN ClientVisit ON C.client_id = ClientVisit.client_id
          INNER JOIN SavedVisitAnswer ON SavedVisitAnswer.clientvisit_id = ClientVisit.clientvisit_id
          INNER JOIN Question ON Question.question_id = SavedVisitAnswer.question_id
          INNER JOIN Category ON Question.category_id = Category.category_id
          INNER JOIN FormVersion ON Category.form_ver_id = FormVersion.form_ver_id
          INNER JOIN Forms ON Forms.form_id = FormVersion.form_id
          INNER JOIN Answer ON SavedVisitAnswer.answer_id = Answer.answer_id
          WHERE Forms.form_id = @FormId
          AND FormVersion.is_active = '1'
          AND ClientVisit.rev_timein >= @DateFrom
          AND ClientVisit.rev_timein < DATEADD(DAY, 1, @DateTo)
          GROUP BY
          C.client_id
          ,C.first_name
          ,C.last_name
          ,ClientVisit.rev_timein


          EDIT I removed all variables. including your @param1 and @param2. If you want to use them put them back and execute it:



          SELECT 
          C.client_id
          ,C.first_name
          ,C.last_name
          ,ClientVisit.rev_timein
          ,MAX(CASE WHEN SavedVisitAnswer.question_id = 518722 THEN Answer.Answer ELSE '' END) Answer1
          ,MAX(CASE WHEN SavedVisitAnswer.question_id = 518727 THEN Answer.Answer ELSE '' END) Answer2
          ,MAX(CASE WHEN SavedVisitAnswer.question_id = 518728 THEN Answer.Answer ELSE '' END) Answer3
          FROM Clients C
          INNER JOIN ClientVisit ON C.client_id = ClientVisit.client_id
          INNER JOIN SavedVisitAnswer ON SavedVisitAnswer.clientvisit_id = ClientVisit.clientvisit_id
          INNER JOIN Question ON Question.question_id = SavedVisitAnswer.question_id
          INNER JOIN Category ON Question.category_id = Category.category_id
          INNER JOIN FormVersion ON Category.form_ver_id = FormVersion.form_ver_id
          INNER JOIN Forms ON Forms.form_id = FormVersion.form_id
          INNER JOIN Answer ON SavedVisitAnswer.answer_id = Answer.answer_id
          WHERE Forms.form_id = 336
          AND FormVersion.is_active = '1'
          AND ClientVisit.rev_timein >= '2018-11-01' /*@param1?*/
          AND ClientVisit.rev_timein < DATEADD(DAY, 1, '2018-11-30' /*param2?*/)
          GROUP BY
          C.client_id
          ,C.first_name
          ,C.last_name
          ,ClientVisit.rev_timein





          share|improve this answer


























          • Just to be clear, your DECLARE and SET statements are just for cleanliness? I ask because I cannot use them as the interface I have access to requires SELECT to be the very first thing, I cannot even have white space before it. I'm going to give this a try though, I just wanted to make sure.

            – Bryan
            Nov 21 '18 at 15:36











          • @Bryan , I saw param1 and param2 in your script, because of that I thought you can use variables. Anyway, I have added another query with no variable including param1 and param2. Change the dates and and execute it.

            – Zeki Gumus
            Nov 21 '18 at 16:06











          • Ok, that's what I thought. param1, param2 and param3 are generated by the web front-end so I have access to a total of 3 variables. As you can see they make it real easy to do reports(sarcasm). I am getting an error with query and it's possible I may have to contact the EMR provider as I think it's a bug. It's telling me "Ambiguous column name 'Answer'. " and Answer isn't a column it's a table. Unless you have some insight I'm going to assume this is a bug in there interpreter.

            – Bryan
            Nov 21 '18 at 16:18













          • @Bryan , So, you have Answer field in more than one table that you use in join. I have updated Answer as Answer.Answer. Try again.

            – Zeki Gumus
            Nov 21 '18 at 16:25













          • Thank you very much. This is much simpler and makes a lot of sense. I hope that as I work with this more it becomes easier and my queries less messy. I was actually hired on as a 'Programmer' and it turns out they really just wanted an SQL Report Writer.

            – Bryan
            Nov 21 '18 at 18:02
















          0














          You don't need to do three sub query for what you need. Also you don't need to filter in the last where clause because you already did it in the sub-queries. I tried to create simple solution for you. If you search three single answer for three specific question it should work for you:



          DECLARE @DateFrom    DATE
          DECLARE @DateTo DATE
          DECLARE @QuestionId1 INT
          DECLARE @QuestionId2 INT
          DECLARE @QuestionId3 INT
          DECLARE @FormId INT

          SET @DateFrom ='2018-11-01'
          SET @DateTo ='2018-11-30'
          SET @FormId =336
          SET @QuestionId1 =518722
          SET @QuestionId2 =518727
          SET @QuestionId3 =518728

          SELECT
          C.client_id
          ,C.first_name
          ,C.last_name
          ,ClientVisit.rev_timein
          ,MAX(CASE WHEN SavedVisitAnswer.question_id = @QuestionId1 THEN Answer.Answer ELSE '' END) Answer1
          ,MAX(CASE WHEN SavedVisitAnswer.question_id = @QuestionId2 THEN Answer.Answer ELSE '' END) Answer2
          ,MAX(CASE WHEN SavedVisitAnswer.question_id = @QuestionId3 THEN Answer.Answer ELSE '' END) Answer3
          FROM Clients C
          INNER JOIN ClientVisit ON C.client_id = ClientVisit.client_id
          INNER JOIN SavedVisitAnswer ON SavedVisitAnswer.clientvisit_id = ClientVisit.clientvisit_id
          INNER JOIN Question ON Question.question_id = SavedVisitAnswer.question_id
          INNER JOIN Category ON Question.category_id = Category.category_id
          INNER JOIN FormVersion ON Category.form_ver_id = FormVersion.form_ver_id
          INNER JOIN Forms ON Forms.form_id = FormVersion.form_id
          INNER JOIN Answer ON SavedVisitAnswer.answer_id = Answer.answer_id
          WHERE Forms.form_id = @FormId
          AND FormVersion.is_active = '1'
          AND ClientVisit.rev_timein >= @DateFrom
          AND ClientVisit.rev_timein < DATEADD(DAY, 1, @DateTo)
          GROUP BY
          C.client_id
          ,C.first_name
          ,C.last_name
          ,ClientVisit.rev_timein


          EDIT I removed all variables. including your @param1 and @param2. If you want to use them put them back and execute it:



          SELECT 
          C.client_id
          ,C.first_name
          ,C.last_name
          ,ClientVisit.rev_timein
          ,MAX(CASE WHEN SavedVisitAnswer.question_id = 518722 THEN Answer.Answer ELSE '' END) Answer1
          ,MAX(CASE WHEN SavedVisitAnswer.question_id = 518727 THEN Answer.Answer ELSE '' END) Answer2
          ,MAX(CASE WHEN SavedVisitAnswer.question_id = 518728 THEN Answer.Answer ELSE '' END) Answer3
          FROM Clients C
          INNER JOIN ClientVisit ON C.client_id = ClientVisit.client_id
          INNER JOIN SavedVisitAnswer ON SavedVisitAnswer.clientvisit_id = ClientVisit.clientvisit_id
          INNER JOIN Question ON Question.question_id = SavedVisitAnswer.question_id
          INNER JOIN Category ON Question.category_id = Category.category_id
          INNER JOIN FormVersion ON Category.form_ver_id = FormVersion.form_ver_id
          INNER JOIN Forms ON Forms.form_id = FormVersion.form_id
          INNER JOIN Answer ON SavedVisitAnswer.answer_id = Answer.answer_id
          WHERE Forms.form_id = 336
          AND FormVersion.is_active = '1'
          AND ClientVisit.rev_timein >= '2018-11-01' /*@param1?*/
          AND ClientVisit.rev_timein < DATEADD(DAY, 1, '2018-11-30' /*param2?*/)
          GROUP BY
          C.client_id
          ,C.first_name
          ,C.last_name
          ,ClientVisit.rev_timein





          share|improve this answer


























          • Just to be clear, your DECLARE and SET statements are just for cleanliness? I ask because I cannot use them as the interface I have access to requires SELECT to be the very first thing, I cannot even have white space before it. I'm going to give this a try though, I just wanted to make sure.

            – Bryan
            Nov 21 '18 at 15:36











          • @Bryan , I saw param1 and param2 in your script, because of that I thought you can use variables. Anyway, I have added another query with no variable including param1 and param2. Change the dates and and execute it.

            – Zeki Gumus
            Nov 21 '18 at 16:06











          • Ok, that's what I thought. param1, param2 and param3 are generated by the web front-end so I have access to a total of 3 variables. As you can see they make it real easy to do reports(sarcasm). I am getting an error with query and it's possible I may have to contact the EMR provider as I think it's a bug. It's telling me "Ambiguous column name 'Answer'. " and Answer isn't a column it's a table. Unless you have some insight I'm going to assume this is a bug in there interpreter.

            – Bryan
            Nov 21 '18 at 16:18













          • @Bryan , So, you have Answer field in more than one table that you use in join. I have updated Answer as Answer.Answer. Try again.

            – Zeki Gumus
            Nov 21 '18 at 16:25













          • Thank you very much. This is much simpler and makes a lot of sense. I hope that as I work with this more it becomes easier and my queries less messy. I was actually hired on as a 'Programmer' and it turns out they really just wanted an SQL Report Writer.

            – Bryan
            Nov 21 '18 at 18:02














          0












          0








          0







          You don't need to do three sub query for what you need. Also you don't need to filter in the last where clause because you already did it in the sub-queries. I tried to create simple solution for you. If you search three single answer for three specific question it should work for you:



          DECLARE @DateFrom    DATE
          DECLARE @DateTo DATE
          DECLARE @QuestionId1 INT
          DECLARE @QuestionId2 INT
          DECLARE @QuestionId3 INT
          DECLARE @FormId INT

          SET @DateFrom ='2018-11-01'
          SET @DateTo ='2018-11-30'
          SET @FormId =336
          SET @QuestionId1 =518722
          SET @QuestionId2 =518727
          SET @QuestionId3 =518728

          SELECT
          C.client_id
          ,C.first_name
          ,C.last_name
          ,ClientVisit.rev_timein
          ,MAX(CASE WHEN SavedVisitAnswer.question_id = @QuestionId1 THEN Answer.Answer ELSE '' END) Answer1
          ,MAX(CASE WHEN SavedVisitAnswer.question_id = @QuestionId2 THEN Answer.Answer ELSE '' END) Answer2
          ,MAX(CASE WHEN SavedVisitAnswer.question_id = @QuestionId3 THEN Answer.Answer ELSE '' END) Answer3
          FROM Clients C
          INNER JOIN ClientVisit ON C.client_id = ClientVisit.client_id
          INNER JOIN SavedVisitAnswer ON SavedVisitAnswer.clientvisit_id = ClientVisit.clientvisit_id
          INNER JOIN Question ON Question.question_id = SavedVisitAnswer.question_id
          INNER JOIN Category ON Question.category_id = Category.category_id
          INNER JOIN FormVersion ON Category.form_ver_id = FormVersion.form_ver_id
          INNER JOIN Forms ON Forms.form_id = FormVersion.form_id
          INNER JOIN Answer ON SavedVisitAnswer.answer_id = Answer.answer_id
          WHERE Forms.form_id = @FormId
          AND FormVersion.is_active = '1'
          AND ClientVisit.rev_timein >= @DateFrom
          AND ClientVisit.rev_timein < DATEADD(DAY, 1, @DateTo)
          GROUP BY
          C.client_id
          ,C.first_name
          ,C.last_name
          ,ClientVisit.rev_timein


          EDIT I removed all variables. including your @param1 and @param2. If you want to use them put them back and execute it:



          SELECT 
          C.client_id
          ,C.first_name
          ,C.last_name
          ,ClientVisit.rev_timein
          ,MAX(CASE WHEN SavedVisitAnswer.question_id = 518722 THEN Answer.Answer ELSE '' END) Answer1
          ,MAX(CASE WHEN SavedVisitAnswer.question_id = 518727 THEN Answer.Answer ELSE '' END) Answer2
          ,MAX(CASE WHEN SavedVisitAnswer.question_id = 518728 THEN Answer.Answer ELSE '' END) Answer3
          FROM Clients C
          INNER JOIN ClientVisit ON C.client_id = ClientVisit.client_id
          INNER JOIN SavedVisitAnswer ON SavedVisitAnswer.clientvisit_id = ClientVisit.clientvisit_id
          INNER JOIN Question ON Question.question_id = SavedVisitAnswer.question_id
          INNER JOIN Category ON Question.category_id = Category.category_id
          INNER JOIN FormVersion ON Category.form_ver_id = FormVersion.form_ver_id
          INNER JOIN Forms ON Forms.form_id = FormVersion.form_id
          INNER JOIN Answer ON SavedVisitAnswer.answer_id = Answer.answer_id
          WHERE Forms.form_id = 336
          AND FormVersion.is_active = '1'
          AND ClientVisit.rev_timein >= '2018-11-01' /*@param1?*/
          AND ClientVisit.rev_timein < DATEADD(DAY, 1, '2018-11-30' /*param2?*/)
          GROUP BY
          C.client_id
          ,C.first_name
          ,C.last_name
          ,ClientVisit.rev_timein





          share|improve this answer















          You don't need to do three sub query for what you need. Also you don't need to filter in the last where clause because you already did it in the sub-queries. I tried to create simple solution for you. If you search three single answer for three specific question it should work for you:



          DECLARE @DateFrom    DATE
          DECLARE @DateTo DATE
          DECLARE @QuestionId1 INT
          DECLARE @QuestionId2 INT
          DECLARE @QuestionId3 INT
          DECLARE @FormId INT

          SET @DateFrom ='2018-11-01'
          SET @DateTo ='2018-11-30'
          SET @FormId =336
          SET @QuestionId1 =518722
          SET @QuestionId2 =518727
          SET @QuestionId3 =518728

          SELECT
          C.client_id
          ,C.first_name
          ,C.last_name
          ,ClientVisit.rev_timein
          ,MAX(CASE WHEN SavedVisitAnswer.question_id = @QuestionId1 THEN Answer.Answer ELSE '' END) Answer1
          ,MAX(CASE WHEN SavedVisitAnswer.question_id = @QuestionId2 THEN Answer.Answer ELSE '' END) Answer2
          ,MAX(CASE WHEN SavedVisitAnswer.question_id = @QuestionId3 THEN Answer.Answer ELSE '' END) Answer3
          FROM Clients C
          INNER JOIN ClientVisit ON C.client_id = ClientVisit.client_id
          INNER JOIN SavedVisitAnswer ON SavedVisitAnswer.clientvisit_id = ClientVisit.clientvisit_id
          INNER JOIN Question ON Question.question_id = SavedVisitAnswer.question_id
          INNER JOIN Category ON Question.category_id = Category.category_id
          INNER JOIN FormVersion ON Category.form_ver_id = FormVersion.form_ver_id
          INNER JOIN Forms ON Forms.form_id = FormVersion.form_id
          INNER JOIN Answer ON SavedVisitAnswer.answer_id = Answer.answer_id
          WHERE Forms.form_id = @FormId
          AND FormVersion.is_active = '1'
          AND ClientVisit.rev_timein >= @DateFrom
          AND ClientVisit.rev_timein < DATEADD(DAY, 1, @DateTo)
          GROUP BY
          C.client_id
          ,C.first_name
          ,C.last_name
          ,ClientVisit.rev_timein


          EDIT I removed all variables. including your @param1 and @param2. If you want to use them put them back and execute it:



          SELECT 
          C.client_id
          ,C.first_name
          ,C.last_name
          ,ClientVisit.rev_timein
          ,MAX(CASE WHEN SavedVisitAnswer.question_id = 518722 THEN Answer.Answer ELSE '' END) Answer1
          ,MAX(CASE WHEN SavedVisitAnswer.question_id = 518727 THEN Answer.Answer ELSE '' END) Answer2
          ,MAX(CASE WHEN SavedVisitAnswer.question_id = 518728 THEN Answer.Answer ELSE '' END) Answer3
          FROM Clients C
          INNER JOIN ClientVisit ON C.client_id = ClientVisit.client_id
          INNER JOIN SavedVisitAnswer ON SavedVisitAnswer.clientvisit_id = ClientVisit.clientvisit_id
          INNER JOIN Question ON Question.question_id = SavedVisitAnswer.question_id
          INNER JOIN Category ON Question.category_id = Category.category_id
          INNER JOIN FormVersion ON Category.form_ver_id = FormVersion.form_ver_id
          INNER JOIN Forms ON Forms.form_id = FormVersion.form_id
          INNER JOIN Answer ON SavedVisitAnswer.answer_id = Answer.answer_id
          WHERE Forms.form_id = 336
          AND FormVersion.is_active = '1'
          AND ClientVisit.rev_timein >= '2018-11-01' /*@param1?*/
          AND ClientVisit.rev_timein < DATEADD(DAY, 1, '2018-11-30' /*param2?*/)
          GROUP BY
          C.client_id
          ,C.first_name
          ,C.last_name
          ,ClientVisit.rev_timein






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 '18 at 16:24

























          answered Nov 21 '18 at 15:02









          Zeki GumusZeki Gumus

          1,382212




          1,382212













          • Just to be clear, your DECLARE and SET statements are just for cleanliness? I ask because I cannot use them as the interface I have access to requires SELECT to be the very first thing, I cannot even have white space before it. I'm going to give this a try though, I just wanted to make sure.

            – Bryan
            Nov 21 '18 at 15:36











          • @Bryan , I saw param1 and param2 in your script, because of that I thought you can use variables. Anyway, I have added another query with no variable including param1 and param2. Change the dates and and execute it.

            – Zeki Gumus
            Nov 21 '18 at 16:06











          • Ok, that's what I thought. param1, param2 and param3 are generated by the web front-end so I have access to a total of 3 variables. As you can see they make it real easy to do reports(sarcasm). I am getting an error with query and it's possible I may have to contact the EMR provider as I think it's a bug. It's telling me "Ambiguous column name 'Answer'. " and Answer isn't a column it's a table. Unless you have some insight I'm going to assume this is a bug in there interpreter.

            – Bryan
            Nov 21 '18 at 16:18













          • @Bryan , So, you have Answer field in more than one table that you use in join. I have updated Answer as Answer.Answer. Try again.

            – Zeki Gumus
            Nov 21 '18 at 16:25













          • Thank you very much. This is much simpler and makes a lot of sense. I hope that as I work with this more it becomes easier and my queries less messy. I was actually hired on as a 'Programmer' and it turns out they really just wanted an SQL Report Writer.

            – Bryan
            Nov 21 '18 at 18:02



















          • Just to be clear, your DECLARE and SET statements are just for cleanliness? I ask because I cannot use them as the interface I have access to requires SELECT to be the very first thing, I cannot even have white space before it. I'm going to give this a try though, I just wanted to make sure.

            – Bryan
            Nov 21 '18 at 15:36











          • @Bryan , I saw param1 and param2 in your script, because of that I thought you can use variables. Anyway, I have added another query with no variable including param1 and param2. Change the dates and and execute it.

            – Zeki Gumus
            Nov 21 '18 at 16:06











          • Ok, that's what I thought. param1, param2 and param3 are generated by the web front-end so I have access to a total of 3 variables. As you can see they make it real easy to do reports(sarcasm). I am getting an error with query and it's possible I may have to contact the EMR provider as I think it's a bug. It's telling me "Ambiguous column name 'Answer'. " and Answer isn't a column it's a table. Unless you have some insight I'm going to assume this is a bug in there interpreter.

            – Bryan
            Nov 21 '18 at 16:18













          • @Bryan , So, you have Answer field in more than one table that you use in join. I have updated Answer as Answer.Answer. Try again.

            – Zeki Gumus
            Nov 21 '18 at 16:25













          • Thank you very much. This is much simpler and makes a lot of sense. I hope that as I work with this more it becomes easier and my queries less messy. I was actually hired on as a 'Programmer' and it turns out they really just wanted an SQL Report Writer.

            – Bryan
            Nov 21 '18 at 18:02

















          Just to be clear, your DECLARE and SET statements are just for cleanliness? I ask because I cannot use them as the interface I have access to requires SELECT to be the very first thing, I cannot even have white space before it. I'm going to give this a try though, I just wanted to make sure.

          – Bryan
          Nov 21 '18 at 15:36





          Just to be clear, your DECLARE and SET statements are just for cleanliness? I ask because I cannot use them as the interface I have access to requires SELECT to be the very first thing, I cannot even have white space before it. I'm going to give this a try though, I just wanted to make sure.

          – Bryan
          Nov 21 '18 at 15:36













          @Bryan , I saw param1 and param2 in your script, because of that I thought you can use variables. Anyway, I have added another query with no variable including param1 and param2. Change the dates and and execute it.

          – Zeki Gumus
          Nov 21 '18 at 16:06





          @Bryan , I saw param1 and param2 in your script, because of that I thought you can use variables. Anyway, I have added another query with no variable including param1 and param2. Change the dates and and execute it.

          – Zeki Gumus
          Nov 21 '18 at 16:06













          Ok, that's what I thought. param1, param2 and param3 are generated by the web front-end so I have access to a total of 3 variables. As you can see they make it real easy to do reports(sarcasm). I am getting an error with query and it's possible I may have to contact the EMR provider as I think it's a bug. It's telling me "Ambiguous column name 'Answer'. " and Answer isn't a column it's a table. Unless you have some insight I'm going to assume this is a bug in there interpreter.

          – Bryan
          Nov 21 '18 at 16:18







          Ok, that's what I thought. param1, param2 and param3 are generated by the web front-end so I have access to a total of 3 variables. As you can see they make it real easy to do reports(sarcasm). I am getting an error with query and it's possible I may have to contact the EMR provider as I think it's a bug. It's telling me "Ambiguous column name 'Answer'. " and Answer isn't a column it's a table. Unless you have some insight I'm going to assume this is a bug in there interpreter.

          – Bryan
          Nov 21 '18 at 16:18















          @Bryan , So, you have Answer field in more than one table that you use in join. I have updated Answer as Answer.Answer. Try again.

          – Zeki Gumus
          Nov 21 '18 at 16:25







          @Bryan , So, you have Answer field in more than one table that you use in join. I have updated Answer as Answer.Answer. Try again.

          – Zeki Gumus
          Nov 21 '18 at 16:25















          Thank you very much. This is much simpler and makes a lot of sense. I hope that as I work with this more it becomes easier and my queries less messy. I was actually hired on as a 'Programmer' and it turns out they really just wanted an SQL Report Writer.

          – Bryan
          Nov 21 '18 at 18:02





          Thank you very much. This is much simpler and makes a lot of sense. I hope that as I work with this more it becomes easier and my queries less messy. I was actually hired on as a 'Programmer' and it turns out they really just wanted an SQL Report Writer.

          – Bryan
          Nov 21 '18 at 18:02


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414506%2fneed-help-sorting-out-my-join-statements-for-multiple-sub-queries%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Paul Cézanne

          UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

          Angular material date-picker (MatDatepicker) auto completes the date on focus out