人员表P,客户表C,客户额外信息表O只说有用内容人员表中 有id(个人编码),pid(部门编码)
1 p1
2 p2
客户表中,有id(客户编码),perID(客户所属人员编码,跟人员表id对应),comedate(客户注册时间),type(客户类型:A,B,C三种类型)
1 1 2002-10-10 A
2 1 2003-10-10 B
3 2 2003-10-10 C客户额外信息表中,有cid(对应客户编码),cnum(客户天数)
1 33
2 44设定 T  = datediff(day,C.comedate,getDate())
设定 TS = cnum客户表C和客户额外信息表O 一一对应,一个人有很多客户;客户表几十万数据,客户额外信息表同样是几十万数据,这两个表处于高度使用状态中;人员表 几百条数据问题:求所有部门的 客户 T 在 30,60,90,120,150,180,360,∝的个数;TS在  30,60,90,120,150,180,360,∝的个数!(类似于分布状况)
也就是求:A类型客户 p1部门 在T小于30的 有多少家,30到60的有多少家......;p2部门 在T小于30的有多少家........;TS同样的要求;
B类型客户 p1部门 在T小于30的 有多少家,30到60的有多少家......;p2部门 在T小于30的有多少家........;TS同样的要求;
C类型客户 p1部门 在T小于30的 有多少家,30到60的有多少家......;p2部门 在T小于30的有多少家........;TS同样的要求;要一个存储过程搞定3种客户类型的T及TS分布数量~速度越快越好,因为客户表和客户额外信息表经常使用,搞不好就会锁住~
高手帮忙~多谢~

解决方案 »

  1.   

    我自己写的大概样子就是这样的,可是效率不高,帮改改也成select 
    c.perID
    ,c.type
    ,c.id as cusid
    ,datediff(day,c.comedate,getDate())  as T
    ,o.cnum as TS

    into #temp
    from c,o,p
    where c.id=o.cid
    and c.perID=p.id
    and ((@pid<>'' and p.pid=@pID) or (@pid='' and 1=1))
    select p.id as perID
    ,p.pid
    --B
    ,(select count(cusid) from #temp where type='B' and #temp.perID=p.id and T<30              ) as BT30
    ,(select count(cusid) from #temp where type='B' and #temp.perID=p.id and T>=30 and T<60    ) as BT60
    ,(select count(cusid) from #temp where type='B' and #temp.perID=p.id and T>=60 and T<90    ) as BT90
    ,(select count(cusid) from #temp where type='B' and #temp.perID=p.id and T>=90 and T<120   ) as BT120
    ,(select count(cusid) from #temp where type='B' and #temp.perID=p.id and T>=120 and T<150  ) as BT150
    ,(select count(cusid) from #temp where type='B' and #temp.perID=p.id and T>=150 and T<180  ) as BT180
    ,(select count(cusid) from #temp where type='B' and #temp.perID=p.id and T>=180 and T<360  ) as BT360
    ,(select count(cusid) from #temp where type='B' and #temp.perID=p.id and T>=360            ) as BTmax ,(select count(cusid) from #temp where type='B' and #temp.perID=p.id and TS<30             ) as BTS30
    ,(select count(cusid) from #temp where type='B' and #temp.perID=p.id and TS>=30 and TS<60  ) as BTS60
    ,(select count(cusid) from #temp where type='B' and #temp.perID=p.id and TS>=60 and TS<90  ) as BTS90
    ,(select count(cusid) from #temp where type='B' and #temp.perID=p.id and TS>=90 and TS<120 ) as BTS120
    ,(select count(cusid) from #temp where type='B' and #temp.perID=p.id and TS>=120 and TS<150) as BTS150
    ,(select count(cusid) from #temp where type='B' and #temp.perID=p.id and TS>=150 and TS<180) as BTS180
    ,(select count(cusid) from #temp where type='B' and #temp.perID=p.id and TS>=180 and TS<360) as BTS360
    ,(select count(cusid) from #temp where type='B' and #temp.perID=p.id and TS>=360           ) as BTSmax
    --A 
    ......
    from p,#temp
    where p.id=#temp.perID


    group by p.id,p.pid
      

  2.   

    楼主用临时表,且多个子查询,而且对表关联多次,效率肯定不会高的。试试下面的SQL,我这边没有这么大数据量做测试。也来学习一下。select a.type,a.pid,a.customer_category,count(1) as customer_number
    from
    (
      select p.id,p.pid,c.id,c.type,
        case when datediff(day,C.comedate,getDate())<30 then customer_num<30
             when datediff(day,C.comedate,getDate())>=30 and datediff(day,C.comedate,getDate())<60 then 30<=customer_num<60
             when datediff(day,C.comedate,getDate())>=60 and datediff(day,C.comedate,getDate())<90 then 60<=customer_num<90
             when datediff(day,C.comedate,getDate())>=90 and datediff(day,C.comedate,getDate())<120 then 90<=customer_num<120
             when datediff(day,C.comedate,getDate())>=120 and datediff(day,C.comedate,getDate())<150 then 120<=customer_num<150
             when datediff(day,C.comedate,getDate())>=150 and datediff(day,C.comedate,getDate())<180 then 150<=customer_num<180
             when datediff(day,C.comedate,getDate())>=180 and datediff(day,C.comedate,getDate())<360 then 180<=customer_num<360
             when datediff(day,C.comedate,getDate())>=360 then customer_num>=360
        end as customer_category
      from
        p,c
      where p.id=c.perid
    ) a
    group by a.type,a.pid,a.customer_category楼主可以适当建立索引加快查询速度。至于楼主所说的传参数的问题我没太看明白,感觉应该可以类似解决。TS可以类似解决,我这边没有测试数据,所以就没有写了。