涉及LOOP和QUERY的复杂SQL

时间:2019-01-04 14:00:17

标签: sql oracle

我有一些查询的结果,该查询只有一列,在这一列中,我需要逐行传递,以查询该行的内容,最后我需要一个包含所有查询结果的表逐行

我正在使用SQL Developer和Oracle DB上的连接

这重现了我的第一选择

SELECT DISTINCT REGEXP_SUBSTR ('5256,5257,5258,5259','[^,]+',1,LEVEL) NUM_PROD
FROM dual CONNECT BY REGEXP_SUBSTR ('5256,5257,5258,5259','[^,]+',1,LEVEL) IS NOT NULL

我需要获取例如5256的内容,并在另一个查询中使用。

这将返回如下结果:

NAME      | NUM_PROD | COLUMN1 | COLUMN2

Michael | 5256 | AA | BB

所有查询的结果都需要类似

Michael | 5256 | AA | BB

Rachel  | 5257| AA | BB

Jony      | 5258| AA | CC

感谢您的帮助。

编辑:

让我更好地解释一下我的结构如何工作

create table tableA (cod_person, nam_person, num_atend, num_presc, columnX, ColumnY) as (
    select 100, 'Michael', 3300, 2215, 'XX', 'YY' from dual union all
    select 101, 'Rachel',  3301, 2351, 'XX', 'YY' from dual union all
    select 103, 'Jony',    3302, 2463, 'XX', 'YY' from dual union all
    select 104, 'Tony',    3303, 2235, 'XX', 'YY' from dual);


create table tableB (num_presc, num_seq, columnXX, ColumnYY) as (
    select 2215, 2332,'XX', 'YY' from dual union all
    select 2351, 2334,'XX', 'YY' from dual union all
    select 1531, 2345,'XX', 'YY' from dual union all
    select 3250, 2348,'XX', 'YY' from dual);


create table tableC (num_presc, num_prod, num_seq_mat, columnXXX, ColumnYYY) as (
    select 2215, 5256, 2332,'XX', 'YY' from dual union all
    select 1205, 5252, 2337,'XX', 'YY' from dual union all
    select 2351, 5258, 2334,'XX', 'YY' from dual union all
    select 3135, 5260, 2349,'XX', 'YY' from dual);

我的查询必须像

SELECT
    aa.cod_person,
    aa.nam_person,
    aa.num_atend,
    num_prod
FROM (SELECT DISTINCT regexp_substr ('5256,5257,5258,5259','[^,]+',1,level) num_prod FROM dual CONNECT BY regexp_substr ('5256,5257,5258,5259','[^,]+',1,level) IS NOT NULL)
    left join aa using (tableA)
    left join bb using (tableB)
    left join cc using (tableC)
WHERE   bb.num_presc = cc.num_presc 
AND     bb.num_seq = cc.num_seq_mat
AND     cc.num_prod =  (HERE WHEN I AM QUERYING ONE BY ONE I PUT THE NUM_PROD BUT NOW I'M SEARCHING ALL IN ONE TIME, SO, I DONT KNOW IF NEEDED THIS ARGUMENT)
AND     aa.num_presc = bb.num_presc
group by aa.cod_person, aa.num_atend

当前,我一个接一个地查询并使用以下代码:

SELECT
    aa.cod_person,
    aa.nam_person,
    aa.num_atend,
    5256 num_prod
FROM    tableA aa,
        tableB bb,
        tableC cc
WHERE   bb.num_presc = cc.num_presc 
AND     bb.num_seq = cc.num_seq_mat
AND     cc.num_prod =  5256
AND     aa.num_presc = bb.num_presc
group by aa.cod_person, aa.num_atend

但是我需要所有结果

1 个答案:

答案 0 :(得分:1)

这里不需要任何循环。只需将查询与其他数据联接(或左联接),它可以是表或其他查询:

select n.num_prod, o.name, o.column1, o.column2
  from (your_complex_query) n
  join (some_other_query) o on o.some_id = n.num_prod

例如,假设我们还有另外两个表:

create table emp (num_prod, name, dept_no) as (
    select 5256, 'Michael', 'SL' from dual union all
    select 5257, 'Rachel',  'SL' from dual union all
    select 5258, 'Jony',    'IT' from dual union all
    select 5600, 'Tony',    'AC' from dual);

create table dep (dept_no, description) as (
    select 'AC', 'Accounting'    from dual union all
    select 'SL', 'Sales'         from dual union all
    select 'IT', 'IT Department' from dual);

所以我们的查询将是:

select num_prod, emp.name, dept_no, dep.description
  from (
    select distinct regexp_substr ('5256,5257,5258,5259','[^,]+',1,level) num_prod
      from dual 
      connect by regexp_substr ('5256,5257,5258,5259','[^,]+',1,level) is not null)
  left join emp using (num_prod)
  left join dep using (dept_no);

或:

select n.num_prod, d.name, d.dept_no, d.description
  from (
    select distinct regexp_substr ('5256,5257,5258,5259','[^,]+',1,level) num_prod
      from dual 
      connect by regexp_substr ('5256,5257,5258,5259','[^,]+',1,level) is not null) n
  left join (select * from emp join dep using (dept_no)) d on d.num_prod = n.num_prod;

结果:

NUM_PROD  NAME    DEPT_NO DESCRIPTION
--------- ------- ------- -------------
5257      Rachel  SL      Sales
5256      Michael SL      Sales
5258      Jony    IT      IT Department
5259 

编辑:

我在您编辑的问题中创建了示例表。您需要的确切查询是:

select distinct aa.cod_person, aa.nam_person, aa.num_atend, nps.num_prod
from (select distinct regexp_substr ('5256,5257,5258,5259','[^,]+',1,level) num_prod 
        from dual 
        connect by regexp_substr ('5256,5257,5258,5259','[^,]+',1,level) IS NOT NULL) nps
    left join tablec cc on cc.num_prod = nps.num_prod
    left join tableb bb on bb.num_presc = cc.num_presc and bb.num_seq = cc.num_seq_mat 
    left join tablea aa on aa.num_presc = bb.num_presc

以正确的顺序联接表,建立适当的联接条件并为num_prod生成器使用别名,我在查询中使用了nps。结果是:

COD_PERSON NAM_PERSON  NUM_ATEND NUM_PROD
---------- ---------- ---------- -------------------
                                 5259
       100 Michael          3300 5256
       101 Rachel           3301 5258
                                 5257