Solved

SQL Sum

Posted on 2014-03-22
2
499 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
Comment Utility
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
Comment Utility
thank you
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

744 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

15 Experts available now in Live!

Get 1:1 Help Now