Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I try to get the number of quantities from one table but am getting error like this At most one record can return by sub query ? What is wrong with my code ?

SQL
SELECT ProductCode, InitialStock, (SELECT Quantity FROM NewInvoice_2 WHERE NewInvoice_2.Item=Product.ProductCode) AS StockRemaining
FROM Product;


this is my Product table
SQL
PoductId     ProductName    ProductCode    InitialStock
1            product1       P101           10
2            product2       p102           15        


this is my invoice table
SQL
InvoiceNo    ProductCode    Qty
Inv-111      P101           3
Inv-112      P102           5
Inv-113      P102           3


Now i need to get result like this
SQL
ProductCode    InitialStock   StockRemaining
P101           10             7
P102           15             7


I need to get result like this. How to form query for this task ? Help me

Thanks in Advance
Srihari
Posted

1 solution

Try this:
SQL
select p.productcode, initialstock, initialstock - sum(qty) AS stockremaining
from product p join invoice i on p.productcode = i.productcode group by p.productcode, initialstock
 
Share this answer
 
Comments
srihari1904 30-Mar-14 5:08am    
Thankyou that's working fine
Peter Leow 30-Mar-14 7:22am    
You are welcome.
Maciej Los 30-Mar-14 7:05am    
+5!
Peter Leow 30-Mar-14 7:23am    
Thanks Maciej.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900