简体   繁体   English

在Oracle SQL查询中使用计数

[英]Use of count in Oracle SQL Query

I have created a query which gives two column. 我创建了一个查询,该查询给出了两列。 one is of the organization id and one is of person id. 一种是机构ID,另一种是人员ID。 Now I want that those organizations should be retrieved which returns more than one person id. 现在,我希望检索那些返回一个以上ID的组织。

How can I modify my below query by using count tht this can be retrieved 如何通过使用count修改下面的查询,可以将其检索

Query :- 查询:

  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

Output 输出量

Person_id  organization_id
123        400
678        400

So now how can I apply count in such a manner that having count(*) >1 can be used in the above query? 那么,现在如何以上述查询中使用count(*)> 1的方式应用count? That this organization_id will be fetched 该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
 )

I Tried the above given query. 我尝试了上面给出的查询。 But am not being able to use having in it. 但是无法使用它。 Also this is just returning the count. 同样,这只是返回计数。 I need to retrieve the organization_id as well. 我还需要检索organization_id。

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;

This will return one row with only the organization_id for the organization with an id of 400 having more than one person. 对于ID为400的组织有一个以上的人员,这将返回仅包含organization_id的一行。

This will give you all organization_id of organizations having more than one different person_id joined: 这将为您提供person_id加入多个不同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

Just a note: shouldn't you add to your query information_type condition as well? 请注意:您是否也不应在查询中添加information_type条件? As far as I remember columns content depends on defined type of information stored. 据我所知,列的内容取决于所存储信息的定义类型。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM