Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Sum

Posted on 2014-03-22
2
Medium Priority
?
536 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 2000 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

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…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

715 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