Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how can i bind two tbls (returning by 2 select queries)
or
retrieve all these data(columns) in single tbl,
i mean that retrieve all these data in single tbl containing 5 coumns instead of 4 (with pname of 2nd select query)

create proc [dbo].[SpCateProducts](@cate_id int)
    as
begin
    select sb.subcate_name, sum(p.qty)
    from subcategory sb 
      join product p on p.subcate_id = sb.subcate_id
    where sb.cate_id = @cate_id
    group by sb.subcate_name

        select top 1 pname from product join subcategory
    on product.subcate_id=
    (
        select top 1 subcate_id
        from subcategory
        where cate_id=3
        order by NEWID()
    )
end
Posted

1 solution

Without the table schema (I remember your previous question[^], but still...) and their relations, and the specification of what you want exactly it is difficult to answer ("bind two tbls" is not even in English - despite the fact that has no meaning in sql).
Still, I have my ideas what you want. Try adding a subquery to your first one:

SQL
select sb.subcate_name, sum(p.qty), (select pr.p_name from product pr where pr.subcate_id = sb.subcate_id) as p_name
    from subcategory sb 
      join product p on p.subcate_id = sb.subcate_id
    where sb.cate_id = @cate_id
    group by sb.subcate_name
 
Share this answer
 
Comments
ashokdamani 14-Oct-12 12:52pm    
thnk U very much......
Maciej Los 14-Oct-12 13:07pm    
Good work, +5!

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