Click here to Skip to main content
15,883,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
1... product (p_id(pk), subcate_id, p_name, p_qty)
2... cate(cate_id(pk), cate_name)
3... subcate(subcate_id(pk), cate_id, subcate_name )


where pk=PRIMARY KAY

here is also the concept of foreign key is used btween product tbl & subcate tbl as well as cate tbl & subcate tbl............ now i want that write a proc that takes the cate_id as argu & returns a tbl that contains some columns, something like that--->

tbl=(subcate_name, total_qty)

where subcate_name contains all those subcate names, which r corresponding to that cate_id, and the every total_qty cell contains the total number of products in that subcate

i hv tried some sql queries, but didnt work(little lack of knowledge in sql)

so just do me a favor, and write a suitable proc as required... please

Thank u in advance
Posted

Try this:
SQL
select s.subcat_name, sum(p-p_qty) as total_qty from product p inner join subcate s where p.subcate_id=s.subcate_id group by s.subcat_name
 
Share this answer
 
v3
Comments
damodara naidu betha 15-Oct-12 1:51am    
My 5..
Zoltán Zörgő 15-Oct-12 1:54am    
Thank you
I'm not entirely sure I completely understand what it is you are trying to do here, but I think you are losing results where you don't have data?

This may be of use to you?
SQL
CREATE PROC schemaname.procsname(
  @SubCateID INT
) AS
BEGIN
  SELECT  s.subcate_name,
          SUM(ISNULL(p.p_Qty,0))TotalQty
  FROM    
  LEFT JOIN Product p
    ON    s.SubcateID = p.SubcateID
  GROUP BY s.subcate_Name
END

The left join will allow the subcate to show when there is no match at Product.

Let me know if I've got the wrong end of the stick with this.
 
Share this answer
 
v3

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