Joining Multiple Tables with 2 aggregated tables





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0















I have seen this question a couple times and it seems like the answer was followed exactly but only my first row of data is correct.



I'm trying to join 2 tables that are aggregated. So data on this query is
correct on the listings table.
{
Select Sum(listings.Quantity) As Cnt, product.ProductId
from product
left join listings on listings.ProductId = product.ProductId And ProductTypeId = 4
Group By product.ProductId
order by ProductId asc;
}



Then the next table using the same Id on the sales table



{
Select Count(sales.SalePrice) as NumSld, Sum(sales.SalePrice) As Sld, ProductId
from product
left join sales on sales.ProductId = product.ProductId And ProductTypeId = 4
Group By product.ProductID
order by Sld DESC;
}



Here is the attemped Join of the tables



{
Select cmb.ProductId, t2.NumSld, t2.Sld
from product pro
Left Join (
Select ProductId, ProductTypeId, Sum(Quantity) As Cnt
from listings
Group By ProductId
) t1 on t1.ProductId = pro.ProductId and t1.ProductTypeId = 4
Left join (
Select ProductId, ProductTypeId, Count(SalePrice) as NumSld, Sum(SalePrice) As Sld
from sales
Group By ProductId
) t2 on t2.ProductId = pro.ProductId and t2.ProductTypeId = 4
order by t2.Sld desc;
}
The data returned on this is incorrect. Only the first row is correct. I think its something simple that I'm missing. I have been stuck on this for days. Can anyone figure it out?









share









New contributor




Scooterdogfl 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 seen this question a couple times and it seems like the answer was followed exactly but only my first row of data is correct.



    I'm trying to join 2 tables that are aggregated. So data on this query is
    correct on the listings table.
    {
    Select Sum(listings.Quantity) As Cnt, product.ProductId
    from product
    left join listings on listings.ProductId = product.ProductId And ProductTypeId = 4
    Group By product.ProductId
    order by ProductId asc;
    }



    Then the next table using the same Id on the sales table



    {
    Select Count(sales.SalePrice) as NumSld, Sum(sales.SalePrice) As Sld, ProductId
    from product
    left join sales on sales.ProductId = product.ProductId And ProductTypeId = 4
    Group By product.ProductID
    order by Sld DESC;
    }



    Here is the attemped Join of the tables



    {
    Select cmb.ProductId, t2.NumSld, t2.Sld
    from product pro
    Left Join (
    Select ProductId, ProductTypeId, Sum(Quantity) As Cnt
    from listings
    Group By ProductId
    ) t1 on t1.ProductId = pro.ProductId and t1.ProductTypeId = 4
    Left join (
    Select ProductId, ProductTypeId, Count(SalePrice) as NumSld, Sum(SalePrice) As Sld
    from sales
    Group By ProductId
    ) t2 on t2.ProductId = pro.ProductId and t2.ProductTypeId = 4
    order by t2.Sld desc;
    }
    The data returned on this is incorrect. Only the first row is correct. I think its something simple that I'm missing. I have been stuck on this for days. Can anyone figure it out?









    share









    New contributor




    Scooterdogfl 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 seen this question a couple times and it seems like the answer was followed exactly but only my first row of data is correct.



      I'm trying to join 2 tables that are aggregated. So data on this query is
      correct on the listings table.
      {
      Select Sum(listings.Quantity) As Cnt, product.ProductId
      from product
      left join listings on listings.ProductId = product.ProductId And ProductTypeId = 4
      Group By product.ProductId
      order by ProductId asc;
      }



      Then the next table using the same Id on the sales table



      {
      Select Count(sales.SalePrice) as NumSld, Sum(sales.SalePrice) As Sld, ProductId
      from product
      left join sales on sales.ProductId = product.ProductId And ProductTypeId = 4
      Group By product.ProductID
      order by Sld DESC;
      }



      Here is the attemped Join of the tables



      {
      Select cmb.ProductId, t2.NumSld, t2.Sld
      from product pro
      Left Join (
      Select ProductId, ProductTypeId, Sum(Quantity) As Cnt
      from listings
      Group By ProductId
      ) t1 on t1.ProductId = pro.ProductId and t1.ProductTypeId = 4
      Left join (
      Select ProductId, ProductTypeId, Count(SalePrice) as NumSld, Sum(SalePrice) As Sld
      from sales
      Group By ProductId
      ) t2 on t2.ProductId = pro.ProductId and t2.ProductTypeId = 4
      order by t2.Sld desc;
      }
      The data returned on this is incorrect. Only the first row is correct. I think its something simple that I'm missing. I have been stuck on this for days. Can anyone figure it out?









      share









      New contributor




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












      I have seen this question a couple times and it seems like the answer was followed exactly but only my first row of data is correct.



      I'm trying to join 2 tables that are aggregated. So data on this query is
      correct on the listings table.
      {
      Select Sum(listings.Quantity) As Cnt, product.ProductId
      from product
      left join listings on listings.ProductId = product.ProductId And ProductTypeId = 4
      Group By product.ProductId
      order by ProductId asc;
      }



      Then the next table using the same Id on the sales table



      {
      Select Count(sales.SalePrice) as NumSld, Sum(sales.SalePrice) As Sld, ProductId
      from product
      left join sales on sales.ProductId = product.ProductId And ProductTypeId = 4
      Group By product.ProductID
      order by Sld DESC;
      }



      Here is the attemped Join of the tables



      {
      Select cmb.ProductId, t2.NumSld, t2.Sld
      from product pro
      Left Join (
      Select ProductId, ProductTypeId, Sum(Quantity) As Cnt
      from listings
      Group By ProductId
      ) t1 on t1.ProductId = pro.ProductId and t1.ProductTypeId = 4
      Left join (
      Select ProductId, ProductTypeId, Count(SalePrice) as NumSld, Sum(SalePrice) As Sld
      from sales
      Group By ProductId
      ) t2 on t2.ProductId = pro.ProductId and t2.ProductTypeId = 4
      order by t2.Sld desc;
      }
      The data returned on this is incorrect. Only the first row is correct. I think its something simple that I'm missing. I have been stuck on this for days. Can anyone figure it out?







      mysql-5.7





      share









      New contributor




      Scooterdogfl 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




      Scooterdogfl 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 1 min ago







      Scooterdogfl













      New contributor




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









      asked 8 mins ago









      ScooterdogflScooterdogfl

      1




      1




      New contributor




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





      New contributor





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






      Scooterdogfl 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
          });


          }
          });






          Scooterdogfl 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%2f234104%2fjoining-multiple-tables-with-2-aggregated-tables%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








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










          draft saved

          draft discarded


















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













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












          Scooterdogfl 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%2f234104%2fjoining-multiple-tables-with-2-aggregated-tables%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

          Ronny Ackermann

          Köttigit

          MySQL 8.0.15 starts normally but any connection hangs