Solved

SQL Sum

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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

623 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