Solved

Returning Total Inventory Quantity from the AdventureWorks Database

Posted on 2006-07-06
2
267 Views
Last Modified: 2008-03-10
I would like to return a single row which returns the total items in stock for a given productID.

Currently my query is as follows:
SELECT s.SalesOrderID,s.SalesOrderDetailID, s.OrderQty, s.ProductID, s.UnitPrice, s.unitPriceDiscount,
 s.LineTotal, pri.Quantity, p.Name as Name

from Sales.SalesOrderDetail s
            INNER JOIN production.product p
ON p.ProductID = s.ProductID            
inner join production.productInventory pri on pri.ProductID = s.ProductID
where s.SalesOrderID =@SalesOrderID

Because a single productID can exist in multiple locations, this query can return multiple rows - which I don't want.
Experts, How can I SUM the pri.Quantity and return a single row??  Thanks in advance!

0
Comment
Question by:BoggyBayouBoy
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 17052435
SELECT s.ProductID, s.UnitPrice, s.unitPriceDiscount, sum(pri.Quantity) as pri_quantity, p.Name as Name
from Sales.SalesOrderDetail s
            INNER JOIN production.product p
ON p.ProductID = s.ProductID            
inner join production.productInventory pri on pri.ProductID = s.ProductID
where s.SalesOrderID =@SalesOrderID
group by s.ProductID, s.UnitPrice, s.unitPriceDiscount, p.Name
0
 
LVL 1

Author Comment

by:BoggyBayouBoy
ID: 17052692
Sweet!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

707 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

20 Experts available now in Live!

Get 1:1 Help Now