有会员表,需要用到的字段如下:
Userid 会员编号
UserName 会员名称
Userlevel 会员等级
ParentUser 上级会员编号订单表,需要用到的字段如下:
OrderID 订单编号
UserID 会员编号
ProductID 产品编号
ProductPrice 产品单价
Number 订购数量
会员表有上下级关系,以Userlevel 和 ParentUser来区分,下级会员的Userlevel 是上级会员的+1
如某会员的Userlevel为3级,那么他的直接下级就是4级现在需要用一个存储过程,输入参数为会员ID,找出这个会员所有直接下级的团队业绩表(团队是指该会员及该会员下属七级会员为一个团队,业绩也就是在订单表中,该会员的订购产品总价),输入结果为:
会员编号 会员名称 团队总业绩

解决方案 »

  1.   


    --看看是不是你要的
    Create table Users(userid varchar(10),username varchar(20),userlevel int,parentuser varchar(10))
    insert into users select '001','A',1,'000'
    insert into users select '002','B',2,'001'
    insert into users select '003','C',2,'001'
    insert into users select '004','D',3,'002'
    insert into users select '005','E',4,'004'Create table orders(orderid varchar(10),userid varchar(10),productid varchar(10),productprice smallmoney,number int)
    insert into orders select '1','000','A1',10.00,5
    insert into orders select '2','001','A2',12.00,6
    insert into orders select '3','002','A3',14.00,7
    insert into orders select '4','003','A4',16.00,8
    insert into orders select '5','004','A5',18.00,9
    insert into orders select '6','005','A6',20.00,10GO
    create  proc usp_test
    @userid varchar(10)
    AS
    set nocount on
       declare @t table(parentuser varchar(10),userid varchar(10),userlevel int,flag int)
       declare @level int,@username varchar(20)
      select @level=userlevel,@username=username from users where userid=@userid
       insert into @t select parentuser,userid,userlevel,0  from users where parentuser=@userid
       while @@rowcount>0
       begin         
             insert into @t select parentuser,userid,userlevel ,1
             from users
             where parentuser in(select userid from @t where flag=0)         update @t set flag=2 where flag=0
             update @t set flag=0 where flag=1
       endselect @userid as userid, @username as username,sum(productprice*number) as total
    from orders
    inner join
    (
     select userid from @t where userlevel<=@level+7
      union all
     select @userid) t
    on orders.userid=t.useridset nocount off
    GOexec usp_test '001'
    /*
    userid     username             total                 
    ---------- -------------------- --------------------- 
    001        A                    660.0000
    */drop table users,orders
    drop proc usp_test
      

  2.   

    团队是指该会员及该会员下属七级会员为一个团队其实只要在Userid   会员编号的编号处理即可简化问题譬如00000为根,五位为一级第一级为00001,ParentUser='00000'第二级为0000100001,ParentUser='00001'第三级为000010000100001,ParentUser='0000100001'要查@strUserid下面的只需要LIKE @strUserid+'%'即可