Combing two LEFT JOINs as one output

How can I create a Table like this in Latex?

Is there a frame of reference in which I was born before I was conceived?

When was drinking water recognized as crucial in marathon running?

How to lift/raise/repair a segment of concrete slab?

Did Amazon pay $0 in taxes last year?

How can I be pwned if I'm not registered on the compromised site?

Make me a metasequence

Are there any other Chaos-worshipping races?

I encountered my boss during an on-site interview at another company. Should I bring it up when seeing him next time?

lead or lag function to get several values, not just the nth

I can't die. Who am I?

A bug in Excel? Conditional formatting for marking duplicates also highlights unique value

Don't know what I’m looking for regarding removable HDDs?

Why do phishing e-mails use faked e-mail addresses instead of the real one?

For the Kanji 校 is the fifth stroke connected to the sixth stroke?

What is the difference between a forward slip and a side slip?

What are all the squawk codes?

How to mitigate "bandwagon attacking" from players?

How do I deal with being jealous of my own players?

Why is working on the same position for more than 15 years not a red flag?

Can we carry rice to Japan?

Which sins are beyond punishment?

Are small insurances worth it

Is it possible to make a clamp function shorter than a ternary in JS?



Combing two LEFT JOINs as one output














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







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







    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







      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







      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







      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



      share






      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 2 mins ago









      TarasTaras

      101




      101




      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-left-joins-as-one-output%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-left-joins-as-one-output%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

          ORA-01691 (unable to extend lob segment) even though my tablespace has AUTOEXTEND onORA-01692: unable to...

          Always On Availability groups resolving state after failover - Remote harden of transaction...

          Circunscripción electoral de Guipúzcoa Referencias Menú de navegaciónLas claves del sistema electoral en...