Using Group By in an SQL Statement

The original Query

Select A.OrderID, A.ProductID, A.UnitPrice, A.Quantity, A.Discount, B.OrderID, B.CustomerID, B.OrderDate, C.CustomerID, C.Country 
From dbo.[Order Details] A 
  inner join dbo.Orders B on B.OrderID = A.OrderID 
  inner join dbo.Customers C on C.CustomerID = B.CustomerID 
Order by C.CustomerID 

Adding Group By

-Select A.OrderID, A.ProductID, A.UnitPrice, A.Quantity, A.Discount, B.OrderID, B.CustomerID, B.OrderDate, C.CustomerID, C.Country 
Select A.ProductID
From dbo.[Order Details] A 
  inner join dbo.Orders B on B.OrderID = A.OrderID 
  inner join dbo.Customers C on C.CustomerID = B.CustomerID 
-Order by C.CustomerID
group by A.ProductID 

Adding the sum

Select A.ProductID,
sum(A.Quantity), sum(A.Quantity * A.UnitPrice)
From dbo.[Order Details] A 
  inner join dbo.Orders B on B.OrderID = A.OrderID 
  inner join dbo.Customers C on C.CustomerID = B.CustomerID 
group by A.ProductID 

Help us improve, Edit this page on GitHub
or email us at info@fireflymigration.com