on 2013 Oct 03 11:36 AM
select @p_cp=product_code from product_code where parent_id='cp' --Put his results in the following recursive query with recursive ps(product_code,description,parent_id) as (select product_code,description,parent_id from product_code where parent_id ='@P_CP' --=(select product_code from product_code where parent_id='cp') --How do reads the results of the above, it is necessary to use vernier union all select b.product_code,b.description,b.parent_id from ps p join product_code b on p.product_code=b.parent_id ) select * from ps
Request clarification before answering.
Demand for: display all customers of other sales summary according to the product categories
declare @product_code char(10),@rowNR int set @rownr=1 select product_code,row_number() over(order by parent_id) as rowNR,parent_id into #FT_1 from product_code where parent_id='cp' --Perform statistical line number and display finished the first layer categories //select * from #ft_1 while (1=1) begin select @product_code=product_code from #ft_1 where rownr=@rownr --assignment if (@@rowcount=0) break --How to terminate without a line with recursive ps(product_code,description,parent_id) as (select product_code,description,parent_id from product_code where parent_id=@product_code -According to the first layer of the first child of the classification tree union all select b.product_code,b.description,b.parent_id from ps p join product_code b on p.product_code=b.parent_id ) --Recursive query until the end select shipper.customer_id,sum(sh.quantity),description=@parentid from (((select * from ps --Start with sales BiaoShen customer table product category table splicing statistics according to product category sales quantity union all select product_code,description,parent_id from product_code where product_code=@product_code ) a join part on a.product_code=part.product_code) join shipper_line sh on part.part_id=sh.part_id) join shipper on sh.trans_no=shipper.trans_no group by shipper.customer_id,description set @rownr=@rownr+1 end
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
YES
declare @product_code char(10),@rowNR int set @rownr=1 select product_code,row_number() over(order by parent_id) as rowNR,parent_id into #FT_1 from product_code where parent_id='cp' //select * from #ft_1 while (1=1) begin select @product_code=product_code from #ft_1 where rownr=@rownr if (@@rowcount=0) break with recursive ps(product_code,description,parent_id) as (select product_code,description,parent_id from product_code where parent_id=@product_code union all select b.product_code,b.description,b.parent_id from ps p join product_code b on p.product_code=b.parent_id ) select shipper.customer_id,sum(sh.quantity),description=@parentid from (((select * from ps union all select product_code,description,parent_id from product_code where product_code=@product_code ) a join part on a.product_code=part.product_code) join shipper_line sh on part.part_id=sh.part_id) join shipper on sh.trans_no=shipper.trans_no group by shipper.customer_id,description set @rownr=@rownr+1 end
I am a novice, don't know so write right, please correct
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Why are you writing this with a cursor? Perhaps something like this would be easier:
with recursive ps(product_code,description,parent_id) as (select product_code,description,parent_id from product_code where parent_id in (select product_code from product_code where parent_id='cp') union all select b.product_code,b.description,b.parent_id from ps p join product_code b on p.product_code=b.parent_id ) select * from ps;
begin declare var char(10); set var = 'cp'; with recursive ps(product_code,description,parent_id) as (select product_code,description,parent_id from product_code where parent_id in (select product_code from product_code where parent_id= var) union all select b.product_code,b.description,b.parent_id from ps p join product_code b on p.product_code=b.parent_id ) select * from ps; end
User | Count |
---|---|
74 | |
20 | |
9 | |
8 | |
7 | |
5 | |
5 | |
4 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.