Solved

SQL Sum

Posted on 2014-03-22
2
514 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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

763 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