實施例做的值在1列和Oracle:
create table Requirements
(
project_no integer not null
,hardware varchar2(64)
,software varchar2(64)
);
create table Assignment
(
project_no integer not null
,employee_no integer not null
);
insert into Requirements (project_no, hardware, software)
values (1, 'Mouse', 'SQL');
insert into Requirements (project_no, hardware, software)
values (1, 'Keyboard', '');
insert into Assignment (project_no, employee_no)
values (1, 1);
insert into Assignment (project_no, employee_no)
values (1, 2);
insert into Assignment (project_no, employee_no)
values (1, 3);
insert into Assignment (project_no, employee_no)
values (1, 4);
select r.project_no, a.employee_no, r.hardware, r.software
from Assignment a
inner join Requirements r
on r.project_no = a.project_no;
select nvl(r.project_no, a.project_no) as project_no
, a.employee_no
, r.hardware
, r.software
from
(
select row_number() over (partition by project_no order by employee_no) r
, project_no
, employee_no
from Assignment
) a
full outer join
(
select row_number() over (partition by project_no order by hardware desc, software desc) r
, project_no
, hardware
, software
from Requirements
) r
on r.project_no = a.project_no
and r.r = a.r;
drop table Assignment;
drop table Requirements;
使用SQL Server的示例(對於Oracle可能需要輕微編輯):
create table #Requirements
(
project_no bigint not null
,hardware nvarchar(64)
,software nvarchar(64)
)
create table #Assignment
(
project_no bigint not null
,employee_no bigint not null
)
insert #Requirements
select 1, 'Mouse', 'SQL'
union select 1, 'Keyboard', ''
insert #Assignment
select 1, 1
union select 1, 2
union select 1, 3
union select 1, 4
--your original query
select r.project_no, a.employee_no, r.hardware, r.software
from #Assignment a
inner join #Requirements r
on r.project_no = a.project_no
--query which gives you the result you're after (though not recommended, since this isn't really how you should use SQL)
select isnull(r.project_no, a.project_no) project_no
, a.employee_no
, r.hardware
, r.software
from (select row_number() over (partition by project_no order by employee_no) r , * from #Assignment) a
full outer join (select row_number() over (partition by project_no order by hardware desc, software desc) r , * from #Requirements) r
on r.project_no = a.project_no
and r.r = a.r
drop table #Assignment
drop table #Requirements
請不要使用All_Caps字母。改變你的問題使用適當的情況。每個互聯網禮儀的All_caps意味着大喊大叫。這很煩人。 – randominstanceOfLivingThing
你的意思是你想從你的單一非標準化表格中獲得2張表格? –
將數據包含在基礎表中,並使用查詢來獲得您不想要的結果。 – Laurence