Inner Join with Sum Aggregate function in SQL Server

23,823

You aggregate the desired column & group by the remainder, the fact that the columns are from the result of a join is not relevant in your case;

select
   b.ItemName,
   c.SpecificationName,
   sum(a.Quantity)
from
   tablea a
   inner join tableb b on b.ItemID = a.ItemID
   inner join tablec c on c.SpecificationID = b.SpecificationID
group by
   b.ItemName,
   c.SpecificationName
Share:
23,823
thevan
Author by

thevan

Software Engineering Senior Analyst at Accenture Solutions Private Limited, Chennai, India. Interested in ASP.Net, MVC, Web API, WCF, Web Services, ADO.Net, C#.Net, VB.Net, Entity Framework, MS SQLServer, Angular.js, JavaScript, JQuery, Ajax, HTML and CSS

Updated on June 21, 2020

Comments

  • thevan
    thevan almost 4 years

    I have three tables StockSummary, Item, ItemSpecification. Here I want to join these three tables and to get Sum(StockSummary.Quantity). The main Columns are as follows:

    TableA: StockSummary(ItemID, Quantity)
    TableB: Item(ItemID, ItemName, SpecificationID)
    TableC: ItemSpecification(SpecificationName, SpecificationID)
    

    The desired result should give ItemName, SpecificationName and SUM(Quantity). How to use Aggregate function in Inner Joins?