sql语句请教~

xwt799023 2013-06-07 08:44:41
有三个表,结构如下:
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


...全文
190 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
皮特尔 2013-06-07
  • 打赏
  • 举报
回复
应该是:

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
u010412956 2013-06-07
  • 打赏
  • 举报
回复
引用 7 楼 u010412956 的回复:
[quote=引用 6 楼 HJ_daxian 的回复:] 貌似这。。。有难度么...

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
这样写 就等于 报名日期 就是交费日期 了。。。。。[/quote] 虽然楼主就是这个意思。。感觉需求好怪
u010412956 2013-06-07
  • 打赏
  • 举报
回复
引用 6 楼 HJ_daxian 的回复:
貌似这。。。有难度么...

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
这样写 就等于 报名日期 就是交费日期 了。。。。。
  • 打赏
  • 举报
回复
貌似这。。。有难度么...

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
  • 打赏
  • 举报
回复
以学员表为坐表关联就行咯
u010412956 2013-06-07
  • 打赏
  • 举报
回复
2013-01-03 1 0 2013-01-04 2 0 应该是 2013-01-03 null 0 2013-01-04 null 0 吧????
xwt799023 2013-06-07
  • 打赏
  • 举报
回复
表结构如下


-- 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); 
 



xwt799023 2013-06-07
  • 打赏
  • 举报
回复
学员表合同ID与合同表合同ID关联tab1.htid=het.het_id 合同表ID与交费表中的合同ID关联的het.het_id=hetMony.m_htid
xwt799023 2013-06-07
  • 打赏
  • 举报
回复
是按交费日期、按合同号,交费额汇总

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧