2012-11-18 215 views
-1

我是SQL新手。我有一個小問題 - 這裏就是我得到:刪除重複結果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      

任何想法我怎麼能去這樣做?

+0

請不要使用All_Caps字母。改變你的問題使用適當的情況。每個互聯網禮儀的All_caps意味着大喊大叫。這很煩人。 – randominstanceOfLivingThing

+1

你的意思是你想從你的單一非標準化表格中獲得2張表格? –

+0

將數據包含在基礎表中,並使用查詢來獲得您不想要的結果。 – Laurence

回答

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列的值有什麼使用2

+0

我同意1,000,000%與您的上一條語句 – HLGEM

+1

@HLGEM我唯一能想到的是用戶想要顯示一個屏幕/頁面,其中包含項目摘要,其中包含分配人員列表和所需資源。當然,使用這兩個查詢將是明智的事情... – Laurence

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 
+0

這看起來像SQL Server語法? OP的問題標記爲Oracle – Sathya

+0

好點@Sathya - 我已經修改了答案以表明該示例基於T-SQL。可悲的是,我家裏沒有Oracle DB來測試查詢。 – JohnLBevan

+0

@JohnLBevan:你可以使用SQLFiddle:http://sqlfiddle.com這個語法有幾個與Oracle有關的問題(不選擇,插入沒有,';'終止語句) –