Combing two queries in oneSQL Select taking too much time to executeOne query two MySQL serversAre these two...

Did Amazon pay $0 in taxes last year?

Roots of 6th chords on the guitar for different inversions/voicings

In Adventurer's League, is it possible to keep the Ring of Winter if you manage to acquire it in the Tomb of Annihilation adventure?

What is this waxed root vegetable?

Reason why dimensional travelling would be restricted

If a set is open, does that imply that it has no boundary points?

Pure Functions: Does "No Side Effects" Imply "Always Same Output, Given Same Input"?

Do higher etale homotopy groups of spectrum of a field always vanish?

Source for Cremation Specifically Not Jewish

How to substitute values from a list into a function?

Can a space-faring robot still function over a billion years?

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

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

When was drinking water recognized as crucial in marathon running?

How to evaluate the limit where something is raised to a power of x?

It took me a lot of time to make this, pls like. (YouTube Comments #1)

What happened to QGIS 2.x LTR?

Get length of the longest sequence of numbers with the same sign

Giving a talk in my old university, how prominently should I tell students my salary?

School performs periodic password audits. Is my password compromised?

Starting index at zero

Misplaced tyre lever - alternatives?

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

What am I? I am in theaters and computer programs



Combing two queries in one


SQL Select taking too much time to executeOne query two MySQL serversAre these two queries logically equivalent?Combine multiple similar queries into oneAccess (Jet) SQL: DateTime stamps in TableB flanking each DateTime stamp in TableAQuery with two LEFT JOIN's is not working as expectedGenerating two rows from one source rowSubtract from two MYSQL QueriesFinding gaps on intersection datesHow to improve query performance through denormalization of this schema or other techniques?













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








      edited 31 secs 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 6 mins ago









      TarasTaras

      1011




      1011




      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

          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...