有三个表,结构如下:
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
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
解决方案 »
- 请问这三个题用 PL SQL 怎么写?
- 安装oracle错误,请大神指教
- 我把原来表空间的数据文件备份了,现在重装系统了,如何挂接表空间数据文件。
- oracle 跨表查询 分页(高分悬赏)
- 怎么合并记录呀~~~~~刚揭帖一个,再开一个贴
- 在procedure中动态执行drop和create sequence的语句,报权限不足,急!!!
- 我用SQL PLUS, SELECT 一个有50万条记录的表,为什么会中途停止,将近一个小时候停止
- 各位大哥一个让我头疼了一天的sql语句?
- 请教group by!
- 指定时间(如每个月最后一天)让程序执行DML操作是不是只有触发器才能实现?
- 循环中删除最后一个子分区
- Oracle数据库创建某Job,3秒执行一次SQL语句,结果间隔时间却是3-5秒
合同表ID与交费表中的合同ID关联的het.het_id=hetMony.m_htid
-- 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);
2013-01-04 2 0应该是
2013-01-03 null 0
2013-01-04 null 0吧????
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
from tab1 a left join hetMony b on a.data = b.m_data
group by a.data,a.htid