2013-12-23 50 views
0

我創建了一個查詢,它提供了兩列。一個是組織ID,另一個是人員ID。現在我想要檢索那些返回多個人員ID的組織。在Oracle SQL查詢中使用count

我如何可以通過使用計數THT這可以被檢索

查詢修改我下面的查詢: -

select papf.person_id person_id, 
      hoi.organization_id 


    From hr_organization_information hoi 
      ,per_all_people_f papf 
    where hoi.org_information2 = papf.person_id 
    And hoi.organization_id = 400 
    and sysdate between papf.effective_start_date and papf.effective_end_date 
    group by person_id,organization_id 

輸出

Person_id organization_id 
123  400 
678  400 

所以現在我怎麼能在這樣的申請數量在上面的查詢中可以使用count(*)> 1的方式嗎?這個organization_id將被提取

 select count(PERSON_ID) 
     FROM (
     select papf.person_id person_id, 
     hoi.organization_id 


From hr_organization_information hoi 
     ,per_all_people_f papf 
where hoi.org_information2 = papf.person_id 
And hoi.organization_id = 400 
and sysdate between papf.effective_start_date and papf.effective_end_date 
group by person_id,organization_id 
-- having count(*)>1 
) 

我試過上面給出的查詢。但我無法使用它。這也只是返回計數。我也需要檢索organization_id。

回答

0
select unique hoi.organization_id 
from   hr_organization_information hoi, 
       per_all_people_f papf 
where   hoi.org_information2 = papf.person_id 
and   hoi.organization_id = 400 
and   sysdate between papf.effective_start_date and papf.effective_end_date 
having  count(papf.person_id)>1; 

這將返回一行,只有組織ID爲具有不止一個人的ID爲400的組織。

+0

很高興看到有人知道存在「distinct」關鍵字的同義詞。 :)但我認爲這個查詢會給出錯誤。雖然'having'可以在沒有'group by'子句的情況下使用,但是必須在選擇列表中有聚合,而不是有序列,即使帶有「唯一」前綴。 –

0

這會給你的有一個以上的不同person_id組織所有organization_id加入:

select hoi.organization_id, 
     count(distinct papf.person_id) pers_count 
    from hr_organization_information hoi, per_all_people_f papf 
where hoi.org_information2 = papf.person_id 
    and sysdate between papf.effective_start_date and 
     papf.effective_end_date 
group by organization_id 
having count(distinct papf.person_id) > 1 

只是注意:你不應該添加到您的查詢information_type條件呢?據我記得,列的內容取決於存儲的信息的定義類型。

0
select hoi.organization_id,count(papf.person_id) 
From hr_organization_information hoi inner join per_all_people_f papf 
on hoi.org_information2 = papf.person_id 
where hoi.organization_id = 400 
and sysdate between papf.effective_start_date and papf.effective_end_date 
group by hoi.organization_id having count(papf.person_id)>1;