要查询出下面字段
  跟进人 客户简称 客户全称 开发时间 拜访次数 总开支(元) 状态 产生收益周期(天)从2个表里面获取数据
 客户表 (跟进人 客户简称 客户全称)
 拜访表(跟进人 客户简称 客户全称 拜访时间 开支 状态)
 
 说明:
 开发时间  为第一次拜访时间
 拜访次数  为跟人总共拜访了多少次该客户
 总开发    为跟人每次拜访该客户的费用总和
 状态      为每次拜访有个状态,当状态为 特定的时候,用特定的时候的时间-第一拜访时间= 该产生收益周期(天),
   我想建一个表包括(跟进人 客户简称 客户全称 开发时间 拜访次数 总开支(元) 状态 产生收益周期(天)),然后分别update里面的字段值,
 有 没有什么好的办法,这样速度太慢了

解决方案 »

  1.   

    schema sql script:/*==============================================================*/
    /* Table: customer                                              */
    /*==============================================================*/
    create table customer  (
       cust_id              int                             not null,
       cust_short_name      varchar2(50),
       cust_full_name       varchar2(100),
       constraint PK_CUSTOMER primary key (cust_id)
    );
    /*==============================================================*/
    /* Table: visitor                                               */
    /*==============================================================*/
    create table visitor  (
       visitor_id           int                             not null,
       visitor_name         varchar2(20),
       hire_date            date,
       constraint PK_VISITOR primary key (visitor_id)
    );/*==============================================================*/
    /* Table: cust_visit_his                                        */
    /*==============================================================*/
    create table cust_visit_his  (
       cust_id              int                             not null,
       visitor_id           int                             not null,
       visit_time           date                            not null,
       expense              number(16, 4),
       status               varchar2(3),
       constraint PK_CUST_VISIT_HIS primary key (cust_id, visitor_id, visit_time)
    );alter table cust_visit_his
       add constraint FK_CUST_VIS_REFERENCE_CUSTOMER foreign key (cust_id)
          references customer (cust_id);alter table cust_visit_his
       add constraint FK_CUST_VIS_REFERENCE_VISITOR foreign key (visitor_id)
          references visitor (visitor_id);---------------------------------------------------------------------------------
      

  2.   

    dataINSERT INTO customer VALUES(1, '神教', '神龙教龙行天下');
    INSERT INTO customer VALUES(2, '天宿', '天宿老仙,一统江湖');INSERT INTO visitor VALUES(1, '韦小宝', sysdate);
    INSERT INTO visitor VALUES(2, '段玉', sysdate);INSERT INTO cust_visit_his VALUES(1, 1, date '1998-10-1', 100, '初次');
    INSERT INTO cust_visit_his VALUES(1, 1, date '2000-10-1', 200, '');
    INSERT INTO cust_visit_his VALUES(1, 1, date '2002-10-1', 10000, 'OK');INSERT INTO cust_visit_his VALUES(2, 2, date '1998-10-1', 50, '闯');
    INSERT INTO cust_visit_his VALUES(2, 2, date '2000-10-1', 200, 'OK');COMMIT;
      

  3.   

    below is what's you need. may be you need change or rewrite it.SELECT
       (SELECT visitor_name FROM visitor where visitor_id = x.visitor_id) 跟进人
       ,c.cust_short_name 客户名称
       ,c.cust_full_name 客户全称
       ,x.开发时间
       ,x.拜访次数
       ,x.总开发
       ,x.收益周期
      FROM (
    SELECT 
       visitor_id
      ,cust_id
      ,min(visit_time) 开发时间
      ,count(1) 拜访次数
      ,sum(expense) 总开发
      ,max(visit_time) - min(visit_time) 收益周期
      FROM cust_visit_his A
     GROUP BY visitor_id, cust_id  
       ) x, customer c
     WHERE x.cust_id = c.cust_id(+)
      

  4.   

    codearts 真行啊!佩服你的解答精神。哈!
      

  5.   

    如果状态是4的时候,收益周期 为max(visit_time) - min(visit_time) ,否则为0
    这样写好像不可以
     SELECT
           (SELECT visitor_name FROM visitor where visitor_id = x.visitor_id) 跟进人
           ,c.cust_short_name 客户名称
           ,c.cust_full_name 客户全称
           ,x.开发时间
           ,x.拜访次数
           ,x.总开发
           ,x.收益周期
      FROM (
            SELECT 
                   visitor_id
                  ,cust_id
                  ,min(visit_time) 开发时间
                  ,count(1) 拜访次数
                  ,sum(expense) 总开发
                  ,decode(a.status,4,max(visit_time) - min(visit_time),0) 收益周期
              FROM cust_visit_his A
             GROUP BY visitor_id, cust_id  ,a.status
           ) x, customer c
     WHERE x.cust_id = c.cust_id(+)