Solved

SQL Sum

Posted on 2014-03-22
2
504 Views
Last Modified: 2014-03-22
Hi Experts,

I am looking to sum a total on an order however...  ive no idea how to pull it all together...

I have tried a nested query but it fails on me, not really don't nested queries before so struggling with it... however..

to get the list of results of an order back the SQL works, however... when adding the sum function it fails.

the SQL is

select 
o.OrderNumber, o.WorkOrderNumber, 
s.SupplierName, 
o.SupplierOrderNumber, o.OrderDate, o.DueDeliveryDate,
oi.item, oi.qty,oi.PriceatOrder, oi.itemtotalcost, 
oi.received
from SupplierOrders.Orders o
left join SupplierOrders.Suppliers s on s.SupplierID = o.SupplierID 
left join SupplierOrders.Order_Item oi on oi.OrderNumber = o.OrderNumber--
where 

o.OrderNumber = 27

select sum(oi.itemtotalcost) as OrderTotal 
from SupplierOrders.Orders o
left join SupplierOrders.Suppliers s on s.SupplierID = o.SupplierID 
left join SupplierOrders.Order_Item oi on oi.OrderNumber = o.OrderNumber--
where 
o.OrderNumber = 27

Open in new window

0
Comment
Question by:SimonPrice33
2 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 39947400
Here's an article on SQL Server GROUP BY Solutions, where point #5 'Aggregate AND values from a single row that make up the aggregate:  Subquery' is what you're after.

Since it's a subquery, you were on the right path, and what you needed to pull this off was to include OrderNumber in your subquery, so it can be joined to the main query.

Air code <I do my own stunts too>
SELECT
   o.OrderNumber, o.WorkOrderNumber, 
   s.SupplierName, 
   o.SupplierOrderNumber, o.OrderDate, o.DueDeliveryDate,
   oi.item, oi.qty,oi.PriceatOrder, oi.itemtotalcost, 
   oi.received, 
   tot.OrderTotal
FROM SupplierOrders.Orders o
   LEFT JOIN SupplierOrders.Suppliers s on s.SupplierID = o.SupplierID 
   LEFT JOIN SupplierOrders.Order_Item oi on oi.OrderNumber = o.OrderNumber--
   JOIN (
      SELECT o.OrderNumber, sum(oi.itemtotalcost) as OrderTotal 
      FROM SupplierOrders.Orders o
         LEFT JOIN SupplierOrders.Suppliers s on s.SupplierID = o.SupplierID 
         LEFT JOIN SupplierOrders.Order_Item oi on oi.OrderNumber = o.OrderNumber
      WHERE o.OrderNumber = 27
      GROUP BY o.OrderNumber) tot ON o.OrderNumber = tot.OrderNumber
WHERE o.OrderNumber = 27

Open in new window

0
 

Author Closing Comment

by:SimonPrice33
ID: 39947413
thank you
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now