Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Calculate Total Quantity of Each Product
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

swissivan
Starting Member

30 Posts

Posted - 03/04/2013 :  20:57:58  Show Profile  Reply with Quote
DECLARE @product_ids TABLE (id UNIQUEIDENTIFIER)

-- get all product ids in a PO , e.g. '1111' for ProductA, '2222' for ProductB
INSERT INTO @product_ids(id)
SELECT product_id FROM PurchaseOrder
WHERE number = 'PO1303050001'

I want to get a list of product with total quantity
e.g.
1111 | 2
2222 | 5

but the following code only get total of all products

SELECT id, SUM(quantity) FROM PurchaseOrderProduct WHERE id IN (SELECT * FROM @product_ids))

any comments, thanks.


Edited by - swissivan on 03/04/2013 20:58:21

visakh16
Very Important crosS Applying yaK Herder

India
52326 Posts

Posted - 03/04/2013 :  22:50:33  Show Profile  Reply with Quote

SELECT po.product_id,pop.Qty FROM PurchaseOrder po
INNER JOIN (SELECT id, SUM(quantity) AS Qty FROM PurchaseOrderProduct GROUP BY id) pop
ON pop.id = po.product_id 
WHERE po.number = 'PO1303050001'


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/


Edited by - visakh16 on 03/04/2013 22:50:58
Go to Top of Page

swissivan
Starting Member

30 Posts

Posted - 03/05/2013 :  20:06:16  Show Profile  Reply with Quote
quote:
Originally posted by visakh16


SELECT po.product_id,pop.Qty FROM PurchaseOrder po
INNER JOIN (SELECT id, SUM(quantity) AS Qty FROM PurchaseOrderProduct GROUP BY id) pop
ON pop.id = po.product_id 
WHERE po.number = 'PO1303050001'


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





GROUP BY works great, thanks.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52326 Posts

Posted - 03/06/2013 :  01:02:18  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.04 seconds. Powered By: Snitz Forums 2000