Combing two queries in one












0















I have two tables, e.g. "Locations" and "Connections"



"Locations" has values



Id | Dimension
---------------
1 | 4
2 | 8
3 | 2


"Connections" maintains attributes



Origin | Destination | Value | Distance_KM
-------------------------------------------
1 | 2 | 500 | 30
1 | 3 | 100 | 20
2 | 1 | 100 | 10
2 | 3 | 300 | 10
3 | 1 | 100 | 40


I want to create an output with the following Attribute Table. Where "In" correspond to "Destination" from "Connections" and "Out" to "Origin" accordingly.



Id | Dimension | In_Value | In_Count | In_Dist | Out_Value | Out_Count | Out_Dist
----------------------------------------------------------------------------------
1 | 4 | 200 | 2 | 50 | 600 | 2 | 50
2 | 8 | 500 | 1 | 30 | 400 | 2 | 20
3 | 2 | 400 | 2 | 30 | 100 | 1 | 40


I can achieve the result that I strive for separately with two queries.



Query 1



SELECT C.Destination, SUM(C.Value) AS In_Value, COUNT(C.Destination) AS In_Count, SUM(C.Distance_KM) AS In_Dist
FROM Connections AS C
GROUP BY C.Destination


Query 2



SELECT C.Origin, SUM(C.Value) AS Out_Value, COUNT(C.Origin) AS Out_Count, SUM(C.Origine_KM) AS Out_Dist
FROM Connections AS C
GROUP BY C.Origin


Nevertheless, there should be only one query that solves my issue, is not it? I tried this but no success.



SELECT L.Id AS Id, L.Dimension AS Dimension, C.In_Value, C.In_Count, C.In_Dist, C.Out_Value, C.Out_Count, C.Out_Dist
FROM Locations AS L
LEFT JOIN (
SELECT C.Destination, SUM(C.Value) AS In_Value, COUNT(C.Destination) AS In_Count, SUM(C.Distance_KM) AS In_Dist
FROM Connections AS C
GROUP BY C.Destination
) ON L.Id = C.Destination
LEFT JOIN (
SELECT C.Origin, SUM(C.Value) AS Out_Value, COUNT(C.Origin) AS Out_Count, SUM(C.Origine_KM) AS Out_Dist
FROM Connections AS C
GROUP BY C.Origin
) ON L.Id = C.Origin


Basically, I do not know if I eligible to add a second LEFT JOIN ON to the query with already existing LEFT JOIN ON, am I?





References:




  • Two SQL LEFT JOINS produce incorrect result

  • Multiple left joins on multiple tables in one query










share|improve this question









New contributor




Taras is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    0















    I have two tables, e.g. "Locations" and "Connections"



    "Locations" has values



    Id | Dimension
    ---------------
    1 | 4
    2 | 8
    3 | 2


    "Connections" maintains attributes



    Origin | Destination | Value | Distance_KM
    -------------------------------------------
    1 | 2 | 500 | 30
    1 | 3 | 100 | 20
    2 | 1 | 100 | 10
    2 | 3 | 300 | 10
    3 | 1 | 100 | 40


    I want to create an output with the following Attribute Table. Where "In" correspond to "Destination" from "Connections" and "Out" to "Origin" accordingly.



    Id | Dimension | In_Value | In_Count | In_Dist | Out_Value | Out_Count | Out_Dist
    ----------------------------------------------------------------------------------
    1 | 4 | 200 | 2 | 50 | 600 | 2 | 50
    2 | 8 | 500 | 1 | 30 | 400 | 2 | 20
    3 | 2 | 400 | 2 | 30 | 100 | 1 | 40


    I can achieve the result that I strive for separately with two queries.



    Query 1



    SELECT C.Destination, SUM(C.Value) AS In_Value, COUNT(C.Destination) AS In_Count, SUM(C.Distance_KM) AS In_Dist
    FROM Connections AS C
    GROUP BY C.Destination


    Query 2



    SELECT C.Origin, SUM(C.Value) AS Out_Value, COUNT(C.Origin) AS Out_Count, SUM(C.Origine_KM) AS Out_Dist
    FROM Connections AS C
    GROUP BY C.Origin


    Nevertheless, there should be only one query that solves my issue, is not it? I tried this but no success.



    SELECT L.Id AS Id, L.Dimension AS Dimension, C.In_Value, C.In_Count, C.In_Dist, C.Out_Value, C.Out_Count, C.Out_Dist
    FROM Locations AS L
    LEFT JOIN (
    SELECT C.Destination, SUM(C.Value) AS In_Value, COUNT(C.Destination) AS In_Count, SUM(C.Distance_KM) AS In_Dist
    FROM Connections AS C
    GROUP BY C.Destination
    ) ON L.Id = C.Destination
    LEFT JOIN (
    SELECT C.Origin, SUM(C.Value) AS Out_Value, COUNT(C.Origin) AS Out_Count, SUM(C.Origine_KM) AS Out_Dist
    FROM Connections AS C
    GROUP BY C.Origin
    ) ON L.Id = C.Origin


    Basically, I do not know if I eligible to add a second LEFT JOIN ON to the query with already existing LEFT JOIN ON, am I?





    References:




    • Two SQL LEFT JOINS produce incorrect result

    • Multiple left joins on multiple tables in one query










    share|improve this question









    New contributor




    Taras is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      0












      0








      0








      I have two tables, e.g. "Locations" and "Connections"



      "Locations" has values



      Id | Dimension
      ---------------
      1 | 4
      2 | 8
      3 | 2


      "Connections" maintains attributes



      Origin | Destination | Value | Distance_KM
      -------------------------------------------
      1 | 2 | 500 | 30
      1 | 3 | 100 | 20
      2 | 1 | 100 | 10
      2 | 3 | 300 | 10
      3 | 1 | 100 | 40


      I want to create an output with the following Attribute Table. Where "In" correspond to "Destination" from "Connections" and "Out" to "Origin" accordingly.



      Id | Dimension | In_Value | In_Count | In_Dist | Out_Value | Out_Count | Out_Dist
      ----------------------------------------------------------------------------------
      1 | 4 | 200 | 2 | 50 | 600 | 2 | 50
      2 | 8 | 500 | 1 | 30 | 400 | 2 | 20
      3 | 2 | 400 | 2 | 30 | 100 | 1 | 40


      I can achieve the result that I strive for separately with two queries.



      Query 1



      SELECT C.Destination, SUM(C.Value) AS In_Value, COUNT(C.Destination) AS In_Count, SUM(C.Distance_KM) AS In_Dist
      FROM Connections AS C
      GROUP BY C.Destination


      Query 2



      SELECT C.Origin, SUM(C.Value) AS Out_Value, COUNT(C.Origin) AS Out_Count, SUM(C.Origine_KM) AS Out_Dist
      FROM Connections AS C
      GROUP BY C.Origin


      Nevertheless, there should be only one query that solves my issue, is not it? I tried this but no success.



      SELECT L.Id AS Id, L.Dimension AS Dimension, C.In_Value, C.In_Count, C.In_Dist, C.Out_Value, C.Out_Count, C.Out_Dist
      FROM Locations AS L
      LEFT JOIN (
      SELECT C.Destination, SUM(C.Value) AS In_Value, COUNT(C.Destination) AS In_Count, SUM(C.Distance_KM) AS In_Dist
      FROM Connections AS C
      GROUP BY C.Destination
      ) ON L.Id = C.Destination
      LEFT JOIN (
      SELECT C.Origin, SUM(C.Value) AS Out_Value, COUNT(C.Origin) AS Out_Count, SUM(C.Origine_KM) AS Out_Dist
      FROM Connections AS C
      GROUP BY C.Origin
      ) ON L.Id = C.Origin


      Basically, I do not know if I eligible to add a second LEFT JOIN ON to the query with already existing LEFT JOIN ON, am I?





      References:




      • Two SQL LEFT JOINS produce incorrect result

      • Multiple left joins on multiple tables in one query










      share|improve this question









      New contributor




      Taras is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      I have two tables, e.g. "Locations" and "Connections"



      "Locations" has values



      Id | Dimension
      ---------------
      1 | 4
      2 | 8
      3 | 2


      "Connections" maintains attributes



      Origin | Destination | Value | Distance_KM
      -------------------------------------------
      1 | 2 | 500 | 30
      1 | 3 | 100 | 20
      2 | 1 | 100 | 10
      2 | 3 | 300 | 10
      3 | 1 | 100 | 40


      I want to create an output with the following Attribute Table. Where "In" correspond to "Destination" from "Connections" and "Out" to "Origin" accordingly.



      Id | Dimension | In_Value | In_Count | In_Dist | Out_Value | Out_Count | Out_Dist
      ----------------------------------------------------------------------------------
      1 | 4 | 200 | 2 | 50 | 600 | 2 | 50
      2 | 8 | 500 | 1 | 30 | 400 | 2 | 20
      3 | 2 | 400 | 2 | 30 | 100 | 1 | 40


      I can achieve the result that I strive for separately with two queries.



      Query 1



      SELECT C.Destination, SUM(C.Value) AS In_Value, COUNT(C.Destination) AS In_Count, SUM(C.Distance_KM) AS In_Dist
      FROM Connections AS C
      GROUP BY C.Destination


      Query 2



      SELECT C.Origin, SUM(C.Value) AS Out_Value, COUNT(C.Origin) AS Out_Count, SUM(C.Origine_KM) AS Out_Dist
      FROM Connections AS C
      GROUP BY C.Origin


      Nevertheless, there should be only one query that solves my issue, is not it? I tried this but no success.



      SELECT L.Id AS Id, L.Dimension AS Dimension, C.In_Value, C.In_Count, C.In_Dist, C.Out_Value, C.Out_Count, C.Out_Dist
      FROM Locations AS L
      LEFT JOIN (
      SELECT C.Destination, SUM(C.Value) AS In_Value, COUNT(C.Destination) AS In_Count, SUM(C.Distance_KM) AS In_Dist
      FROM Connections AS C
      GROUP BY C.Destination
      ) ON L.Id = C.Destination
      LEFT JOIN (
      SELECT C.Origin, SUM(C.Value) AS Out_Value, COUNT(C.Origin) AS Out_Count, SUM(C.Origine_KM) AS Out_Dist
      FROM Connections AS C
      GROUP BY C.Origin
      ) ON L.Id = C.Origin


      Basically, I do not know if I eligible to add a second LEFT JOIN ON to the query with already existing LEFT JOIN ON, am I?





      References:




      • Two SQL LEFT JOINS produce incorrect result

      • Multiple left joins on multiple tables in one query







      query aggregate table






      share|improve this question









      New contributor




      Taras is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Taras is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 14 mins ago







      Taras













      New contributor




      Taras is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 20 mins ago









      TarasTaras

      1012




      1012




      New contributor




      Taras is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Taras is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Taras is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "182"
          };
          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: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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
          });


          }
          });






          Taras is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f231424%2fcombing-two-queries-in-one%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Taras is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Taras is a new contributor. Be nice, and check out our Code of Conduct.













          Taras is a new contributor. Be nice, and check out our Code of Conduct.












          Taras is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Database Administrators Stack Exchange!


          • 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%2fdba.stackexchange.com%2fquestions%2f231424%2fcombing-two-queries-in-one%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

          Liste der Baudenkmale in Friedland (Mecklenburg)

          Single-Malt-Whisky

          Czorneboh