有三个表,结构如下:
tab1 学员表id(id)     name(姓名)     dwid(单位ID)       data(报名日期)     htid(合同ID)
    1            张三         2                 2013-01-01                1
   2            李四          2                 2013-01-02                1
   3            王五          2                 2013-01-03                1
   4            王二          2                 2013-01-04                2
   5            王三          2                 2013-01-05                2 het  合同表
het_id(合同id)     name(合同名称)
       1                    培训合同
        2                    就业合同hetMony    学员交费表
mid(id)   m_htid(合同表ID)    m_data(交费日期)    m_money(交费金额)
 1           1               2013-01-01                   200
 2           1               2013-01-01                   300
 3           1               2013-01-02                   700
 4           2               2013-01-05                   300 ---------------------------------------------------------
要求结果如下:   日期 (取学员表报名日期)             合同ID                    (交费金额)
 2013-01-01                        1                          500
 2013-01-02                        1                          700
 2013-01-03                        1                          0
 2013-01-04                        2                          0
 2013-01-05                        2                          300

解决方案 »

  1.   

    学员表合同ID与合同表合同ID关联tab1.htid=het.het_id
    合同表ID与交费表中的合同ID关联的het.het_id=hetMony.m_htid
      

  2.   

    表结构如下
    -- Create table
     create table TAB1
     (
       ID   NUMBER not null,
       NAME VARCHAR2(20),
       DWID NUMBER,
       DATA DATE,
       HTID NUMBER
     )
     tablespace USERS
       pctfree 10
       initrans 1
       maxtrans 255
       storage
       (
         initial 64K
         minextents 1
         maxextents unlimited
       );
     -- Add comments to the table 
     comment on table TAB1
       is '学员表';
    -- Add comments to the columns 
     comment on column TAB1.ID
       is 'id';
     comment on column TAB1.NAME
       is '姓名';
    comment on column TAB1.DWID
       is '单位ID';
     comment on column TAB1.DATA
       is '报名日期';
    comment on column TAB1.HTID
       is '合同ID';
     -- Create/Recreate primary, unique and foreign key constraints 
     alter table TAB1
       add constraint QQ primary key (ID)
       using index 
       tablespace USERS
       pctfree 10
       initrans 2
       maxtrans 255
       storage
       (
         initial 64K
         minextents 1
         maxextents unlimited
       );
     alter table TAB1
       add constraint FA foreign key (HTID)
       references HET (HET_ID);
     
    -- Create table
     create table HET
     (
       HET_ID NUMBER not null,
       NAME   VARCHAR2(30)
     )
     tablespace USERS
       pctfree 10
       initrans 1
       maxtrans 255
       storage
       (
         initial 64K
         minextents 1
         maxextents unlimited
       );
     -- Add comments to the table 
     comment on table HET
       is '合同表';
    -- Add comments to the columns 
     comment on column HET.HET_ID
       is '合同id';
     comment on column HET.NAME
       is '合同名称';
    -- Create/Recreate primary, unique and foreign key constraints 
     alter table HET
       add constraint AAAS primary key (HET_ID)
       using index 
       tablespace USERS
       pctfree 10
       initrans 2
       maxtrans 255
       storage
       (
         initial 64K
         minextents 1
         maxextents unlimited
       );
     
    -- Create table
     create table HETMONY
     (
       MID     NUMBER not null,
       M_HTID  NUMBER,
       M_DATA  DATE,
       M_MONEY NUMBER
     )
     tablespace USERS
       pctfree 10
       initrans 1
       maxtrans 255
       storage
       (
         initial 64K
         minextents 1
         maxextents unlimited
       );
     -- Add comments to the table 
     comment on table HETMONY
       is '学员交费表';
    -- Add comments to the columns 
     comment on column HETMONY.MID
       is 'id';
     comment on column HETMONY.M_HTID
       is '合同表ID';
     comment on column HETMONY.M_DATA
       is '交费日期';
    comment on column HETMONY.M_MONEY
       is '交费金额';
    -- Create/Recreate primary, unique and foreign key constraints 
     alter table HETMONY
       add constraint DS primary key (MID)
       using index 
       tablespace USERS
       pctfree 10
       initrans 2
       maxtrans 255
       storage
       (
         initial 64K
         minextents 1
         maxextents unlimited
       );
     alter table HETMONY
       add constraint F2 foreign key (M_HTID)
       references HETMONY (MID); 
     
      

  3.   

     2013-01-03                        1                          0
     2013-01-04                        2                          0应该是
     2013-01-03                        null                          0
     2013-01-04                        null                          0吧????
      

  4.   

    貌似这有难度么...
    select a.data,a.htid,sum(b.m_money) m_money
    from tab1 a left join hetMony a on a.data = b.m_data
    group by a.data,a.htid
      

  5.   

    应该是:select a.data,a.htid,sum(b.m_money) m_money
    from tab1 a left join hetMony b on a.data = b.m_data
    group by a.data,a.htid