oracle查询以获取计数

时间:2018-12-13 18:41:52

标签: sql oracle plsql

我有一个带有组织名称和rovaling_status的表。 Approval_status可以被批准/拒绝。如何使用Oracle SQL查询和PLSQL过程获取每个组织的批准和拒绝记录的数量

2 个答案:

答案 0 :(得分:1)

以下是将conditional aggregationcase语句一起使用的一种选择:

select organisation, 
       count(case when approval_status = 'approved' then 1 end) as approvedcount,
       count(case when approval_status = 'rejected' then 1 end) as rejectedcount
from yourtable
group by organisation

答案 1 :(得分:0)

对于Oracle的SQL Select语句,一种选择是使用sum列中的decoded,如下所示:

select organisation_name, 
       sum(decode(approval_status,'approved',1,0)) as "Count(Approved)",
       sum(decode(approval_status,'rejected',1,0)) as "Count(Rejected)"
  from tab
 group by organisation_name;

您可以创建一个存储过程,将这些计算出的值作为结果返回:

SQL> set serveroutput on;
SQL> create or replace procedure pr_get_org_count( 
                                             i_org_name tab.organisation_name%type,
                                             o_cnt_approved out pls_integer,
                                             o_cnt_rejected out pls_integer,
                                            ) is

begin
     select sum(decode(approval_status,'approved',1,0)) ,
            sum(decode(approval_status,'rejected',1,0))
       into o_cnt_approved, o_cnt_rejected
       from tab
      where organisation_name = i_org_name;

    dbms_output.put_line(' Approved Count is : '||o_cnt_approved);
    dbms_output.put_line(' Rejected Count is : '||o_cnt_rejected);
    -- these previous two line can be used to show the results through command line.
end;

如果需要为每个组织输出多行,请在过程中的游标中使用最上面分组的select语句,而无需使用参数:

SQL> create or replace procedure pr_get_org_count is    
begin
    for c in
    (  
     select organisation_name,
            sum(decode(approval_status,'approved',1,0)) as cnt_approved ,
            sum(decode(approval_status,'rejected',1,0)) as cnt_rejected
       from tab
      group by organisation_name
    )
    loop
      dbms_output.put(' Organisation Name : '||c.organisation_name);        
      dbms_output.put(' [ Approved Count : '||c.cnt_approved);
      dbms_output.put_line(' - Rejected Count : '||c.cnt_rejected||' ]');
    end loop;
end;

EDIT (与评论有关) 您可以将此过程转换为以下样式,以将结果邮寄到

create or replace procedure pr_get_org_count( 
                                             i_org_name tab.organisation_name%type,
                                             o_cnt_approved out pls_integer,
                                             o_cnt_rejected out pls_integer,
                                            ) is
  v_path  varchar2(4000):='http://www.mycompany.com.tr/main/default.aspx?email=';
  v_email varchar2(4000):='barbaros.ozhan@mycompany.com.tr';
  v_text  varchar2(4000):='My Results | for Approved : ';
  v_url   varchar2(4000);
  v_rep   varchar2(4000);
begin
  select sum(decode(approval_status,'approved',1,0)) ,
         sum(decode(approval_status,'rejected',1,0))
    into o_cnt_approved, o_cnt_rejected
    from tab
   where organisation_name = i_org_name;

   v_text := v_text||to_char(o_cnt_approved)||' - for Rejected : '||to_char(o_cnt_rejected);
   v_url  := v_path||'?email='||v_email||'&email_text='||v_text;
   v_rep  := utl_http.request(utl_url.escape(v_url, false, 'UTF-8'));
end;