在Oracle SQL Query中使用count

时间:2013-12-23 20:11:11

标签: sql oracle

我创建了一个提供两列的查询。一个是组织ID,一个是人id。现在我想要检索那些返回多个人ID的组织。

如何使用count来修改我的下面的查询,这可以检索

查询: -

  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。

3 个答案:

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

这将返回一行,其中只有组织的organization_id,其id为400且具有多个人。

答案 1 :(得分:0)

这将为所有organization_id个组织提供多个不同person_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条件?据我记得,列内容取决于存储的已定义信息类型。

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