删除重复结果sql

时间:2012-11-18 18:28:55

标签: sql oracle

我是SQL的新手。我有一个小问题 - 这就是我得到的:

PROJECT_NO       EMPLOYEE_NO      HARDWARE       SOFTWARE  
------------------------------------------------------------
01                   1            MOUSE          SQL
01                   2            MOUSE          SQL
01                   3            MOUSE          SQL
01                   4            MOUSE          SQL
01                   1            KEYBOARD
01                   2            KEYBOARD
01                   3            KEYBOARD
01                   4            KEYBOARD

员工正在重复自己,因为这个项目需要两种类型的硬件。

以下是表格所包含的内容:

Requirements(
  project_no, 
  hardware, 
  software
 )

Assignment(
  project_no, 
  employee_no
)

这就是我想要的:

PROJECT_NO       EMPLOYEE_NO      HARDWARE       SOFTWARE  
---------------------------------------------------------------------
01                   1            MOUSE          SQL
01                   2            KEYBOARD          
01                   3                      
01                   4                      

有关如何进行此操作的任何想法?

2 个答案:

答案 0 :(得分:2)

您的要求在您的问题中大量不清楚,因为您没有在表格中包含数据或您正在使用的查询来获得您不想要的结果。

但是,我猜这是你想要的:

Select
  x.Project_No,
  x.Employee_No,
  y.Hardware,
  y.Software
From (
  Select
    a.*,
    RowNum As rn
  From
    Assignment a
  ) x Full Outer Join (
    Select
      r.*,
      RowNum As rn
    From
      Requirements r
  ) y on x.rn = y.rn

http://sqlfiddle.com/#!4/88178/6

我觉得我应该指出这是一个非常愚蠢的查询,因为第3列和第4列中的值与第1列和第2列中的值无关。

答案 1 :(得分: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