Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us at FabCon Vienna from September 15-18, 2025, for the ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM. Get registered

Reply
EHaft
New Member

SQL triple joins, or how to distinctly combine order details

Hi there,

 

I'm having some trouble attempting to return the total price for each order placed.

 

I'm working with the following tables:

 

Orders (OrderID, OrderDate, Location)

 

Sales (OrderID, ProductSold, QuantitySold)

 

Products (ProductName, SellPrice)

 

I'd like to distinctly select all unique Orders.OrderID entries, and tabulate the sum of each product sold per order.

My problem is that all values in Orders.OrderID are distinct (e.g. 1-20), but for Sales.OrderID there are multiple values per ID (Sales contains the OrderID in question, what product was sold, and how many; thus there may be multiple products [sales] per one order [orders].

 

Is there a simple solution for this that I'm missing? 

 

What I've got so far:

SELECT DISTINCT
Orders.OrderID, Sales.ProductSold, Sales.QuantitySold*Products.SellPrice AS 'Total Price'
FROM Sales
LEFT JOIN Orders ON Orders.OrderID = Sales.OrderID
LEFT JOIN Products ON Products.ProductName = Sales.ProductSold;

 

This returns multiple 'Total Prices' per 'ProductSold" for each OrderID.

Would there be a method way to sum 'Total Price' for each unique OrderID rather than for each ProductSold?

 

Otherwise:

Is there a way to combine the ProductSold and QuantitySold for each order, while still maintaining references to their respective ProductName and SellPrice? Is there a way to combine the information specific to each unique ID number or a way to concatanate all that information into a new table using SQL alone? 

 

Simply put, I'd like this query to return 20 records describing complete orders rather than 60 records describing each piece of an order.

 

Thanks for your help with this, I appreciate you!

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @EHaft ,

 

You may try:

SELECT 
Orders.OrderID, SUM(Sales.QuantitySold*Products.SellPrice) AS 'Total Price'
FROM Sales
LEFT JOIN Orders ON Orders.OrderID = Sales.OrderID
LEFT JOIN Products ON Products.ProductName = Sales.ProductSold
GROUP BY Orders.OrderID

 

If this statement doesn't work on your end, could you please provide us with the table creation statement and sample data, as well as your expect result?

Note:Please be careful not to include sensitive information)

 

Best regards.
Community Support Team_Caitlyn

View solution in original post

1 REPLY 1
Anonymous
Not applicable

Hi @EHaft ,

 

You may try:

SELECT 
Orders.OrderID, SUM(Sales.QuantitySold*Products.SellPrice) AS 'Total Price'
FROM Sales
LEFT JOIN Orders ON Orders.OrderID = Sales.OrderID
LEFT JOIN Products ON Products.ProductName = Sales.ProductSold
GROUP BY Orders.OrderID

 

If this statement doesn't work on your end, could you please provide us with the table creation statement and sample data, as well as your expect result?

Note:Please be careful not to include sensitive information)

 

Best regards.
Community Support Team_Caitlyn

Helpful resources

Announcements
May FBC25 Carousel

Fabric Monthly Update - May 2025

Check out the May 2025 Fabric update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors