P& C之后的查询是什么?

时间:2018-03-11 06:25:03

标签: sql oracle

我有一个查询

select sum(salary) 
from employees 
where email in ('BERNST','DAUSTIN','VPATABAL') 
and department_id in (90,60) 
and manager_id in (103,102);

所以,我想用每个IN的值的置换组合来编写上面的查询 查询总共应该有12个查询。例如

select sum(salary) 
from employees 
where email = 'BERNST' 
and department_id = 90 
and manager_id = 103;

select sum(salary) 
from employees 
where email = 'BERNST'
and department_id = 90 
and manager_id = 102;

select sum(salary) 
from employees 
where email = 'BERNST'
and department_id = 60 
and manager_id = 103;

select sum(salary)  
from employees 
where email = 'BERNST'
and department_id = 60 
and manager_id = 102;

select sum(salary)  
from employees 
where email = 'BERNST'
and department_id = 90 
and manager_id = 103;

等等。

如何在查询中执行P& C以获得每个IN值如上所述的12个查询?

提前致谢。

2 个答案:

答案 0 :(得分:2)

也许是这样的:

with
  emails as  
  (
     select 'BERNST'  as email  from dual union
     select 'DAUSTIN'  from dual union
     select 'VPATABAL' from dual 
  )
, departments as
  (
     select 90 department_id from dual union
     select 60 from dual
  )
, managers as
  (
     select 103 as manager_id from dual union
     select 102 from dual
  )
select 
  'select sum(salary) from employees where email = '''
  || E.email ||
  ''' and department_id = '
  || D.department_id ||
  ' and manager_id = '
  || M.manager_id ||
  ' ;' 
  as query_
from emails E, departments D, managers M 
;

输出:

QUERY_                                                                                                                     
---------------------------------------------------------------------------------------------------------------------------
select sum(salary) from employees where email = 'BERNST' and department_id = 60 and manager_id = 102 ;                     
select sum(salary) from employees where email = 'DAUSTIN' and department_id = 60 and manager_id = 102 ;                    
select sum(salary) from employees where email = 'VPATABAL' and department_id = 60 and manager_id = 102 ;                   
select sum(salary) from employees where email = 'BERNST' and department_id = 60 and manager_id = 103 ;                     
select sum(salary) from employees where email = 'DAUSTIN' and department_id = 60 and manager_id = 103 ;                    
select sum(salary) from employees where email = 'VPATABAL' and department_id = 60 and manager_id = 103 ;                   
select sum(salary) from employees where email = 'BERNST' and department_id = 90 and manager_id = 102 ;                     
select sum(salary) from employees where email = 'DAUSTIN' and department_id = 90 and manager_id = 102 ;                    
select sum(salary) from employees where email = 'VPATABAL' and department_id = 90 and manager_id = 102 ;                   
select sum(salary) from employees where email = 'BERNST' and department_id = 90 and manager_id = 103 ;                     
select sum(salary) from employees where email = 'DAUSTIN' and department_id = 90 and manager_id = 103 ;                    
select sum(salary) from employees where email = 'VPATABAL' and department_id = 90 and manager_id = 103 ;                   

12 rows selected. 

另见:convention。 (不知道" P& C"代表......)

PL / SQL 代码(根据要求提供)

假设:"员工" table包含列email,department_id和manager_id。我们选择DISTINCT值,并使用交叉连接来获得所有必需的组合。如果需要,您还可以将表名和列名作为参数传递。

create or replace package generate_sql
is
  procedure print_queries ;
end;
/

create or replace package body generate_sql
is
  procedure print_queries
  is
    selectstr constant varchar2(128) :=  'select sum(salary) from employees where email = ''' ;
    andstr1   constant varchar2(128) :=  ''' and department_id = ' ;
    andstr2   constant varchar2(128) :=  ' and manager_id = ' ;
  begin
    for rec_ in (
      select *
      from (
        select distinct email 
        from employees
      ), (
        select distinct department_id
        from employees
      ), ( 
        select distinct manager_id
        from employees
      )
    ) loop
      dbms_output.put_line( 
           selectstr || rec_.email 
        || andstr1   || rec_.department_id
        || andstr2   || rec_.manager_id || ' ;' 
      ) ;
    end loop;
  end print_queries;
end generate_sql;
/

执行

begin
  generate_sql.print_queries ;
end;
/

测试(结果见dbfiddle)

用于收集生成的SQL语句(dbms_output.put_line在dbfiddle中没用):

create table generated_queries ( varchar2(4000) ) ;


-- 3 email addresses, 2 departments, 2 managers -> 12 queries
create table employees as
select 'BERNST' email, 60 department_id, 102 manager_id from dual union all
select 'DAUSTIN' , 90, 103 from dual union all
select 'VPATABAL', 60, 103 from dual
;

-- 4 email addresses, 4 departments, 2 managers -> 32 queries
-- drop table employees;

create table employees as
select 'BERNST' email, 10 department_id, 102 manager_id from dual union all
select 'DAUSTIN' , 20, 103 from dual union all
select 'VPATABAL', 30, 103 from dual union all
select 'SSRIVASTAVA', 40, 102 from dual
;

请参阅dbfiddle dbfiddle here

对于我们中的一些人(包括我自己),仍然不太清楚你想对所有这些查询做什么(不需要告诉我们,但是:-))。相信你知道GROUP BY / ROLLUP / CUBE等可以做些什么:祝你好运!

答案 1 :(得分:1)

我发现12个查询的动态构造特别有用。

如果您想要实际的结果,请使用cross join获取行,并使用left join和聚合来获取最终总结:

with emails as(
      select 'BERNST'  as email  from dual union
      select 'DAUSTIN'  from dual union
      select 'VPATABAL' from dual 
     ),
     departments as (
      select 90 department_id from dual union
      select 60 from dual
     ),
     managers as (
      select 103 as manager_id from dual union
      select 102 from dual
     )
select e.email, d.department_id, m.manager_id, sum(emp.salary)
from emails e cross join
     departments d cross join
     managers m left join
     employees emp
     on emp.email = e.email and emp.department_id = d.department_id and
        emp.manager_id = m.manager_id
group by e.email, d.department_id, m.manager_id;