0

我有这个查询,它的一列是一个子查询,它应该使用 listagg 函数带来一个值列表。该列表的起点为S.ID_ORGAO_INTELIGENCIA值。该列表应该是,它始终具有值。

listagg 函数正在使用一个内联视图,该视图使用窗口函数来创建列表。

select *
from (
         SELECT DISTINCT S.ID_SOLICITACAO,
                         S.NR_PROTOCOLO_SOLICITACAO,
                         S.DH_INCLUSAO,
                         S.ID_USUARIO,
                         U.NR_CPF,
                         OI.ID_MODULO,
                         OI.ID_ORGAO_INTELIGENCIA,
                         OI.NO_ORGAO_INTELIGENCIA,
                         R.ID_ATRIBUICAO,
                         P.ID_PERMISSAO,
                         1 AS            TIPO_NOTIFICACAO,
                         (
                             select LISTAGG(oc6.ID_ORGAO_INTELIGENCIA || '-' || oc6.ord || '-', '; ') WITHIN GROUP (ORDER BY oc6.ord) eai
                             from (
                                      SELECT oc1.ID_ORGAO_INTELIGENCIA,
                                             oc1.ID_ORGAO_INTELIGENCIA_PAI,
                                             oc1.SG_ORGAO_INTELIGENCIA,
                                             rownum as ord
                                      FROM TB_ORGAO_INTERNO oc1
                                      WHERE oc1.DH_EXCLUSAO is null
                                            -- THE VALUE FROM S.ID_ORGAO_INTELIGENCIA IS NOT AVAILBLE HERE
                                      START WITH oc1.ID_ORGAO_INTELIGENCIA = S.ID_ORGAO_INTELIGENCIA
                                      CONNECT BY prior oc1.ID_ORGAO_INTELIGENCIA_PAI = oc1.ID_ORGAO_INTELIGENCIA
                         ) oc6) aproPrec

         FROM TB_SOLICITACAO S
                  INNER JOIN TB_ORGAO_INTERNO OI ON S.ID_ORGAO_INTELIGENCIA = OI.ID_ORGAO_INTELIGENCIA
                  INNER JOIN TB_RELACIONAMENTO_ATRIBUICAO R
                             ON (R.ID_MODULO = OI.ID_MODULO AND R.ID_ORGAO_INTELIGENCIA IS NULL AND
                                 R.ID_SOLICITACAO IS NULL)
                  INNER JOIN TB_PERMISSAO P
                             ON (P.ID_USUARIO = :usuario AND P.ID_ORGAO_INTELIGENCIA = :orgao AND
                                 P.ID_ATRIBUICAO = R.ID_ATRIBUICAO)
                  INNER JOIN TB_USUARIO U ON (U.ID_USUARIO = S.ID_USUARIO)
         WHERE 1 = 1
           AND U.DH_EXCLUSAO IS NULL
           AND P.DH_EXCLUSAO IS NULL
           AND S.DH_EXCLUSAO IS NULL
           AND OI.DH_EXCLUSAO IS NULL
           AND R.ID_ATRIBUICAO IN :atribuicoes
           AND P.ID_STATUS_PERMISSAO = 7
           AND OI.ID_MODULO = 1
           AND S.ID_STATUS_SOLICITACAO IN (1, 2, 5, 6)
           and s.ID_ORGAO_INTELIGENCIA in (SELECT DISTINCT o.ID_ORGAO_INTELIGENCIA
                                           FROM TB_ORGAO_INTERNO o
                                           WHERE o.DH_EXCLUSAO IS NULL
                                           START WITH o.ID_ORGAO_INTELIGENCIA = 3
                                           CONNECT BY PRIOR o.ID_ORGAO_INTELIGENCIA = o.ID_ORGAO_INTELIGENCIA_PAI)
     );

问题是该aproPrec列总是null作为其结果返回。

如果我强制标准进行S.ID_ORGAO_INTELIGENCIA硬编码,则列表将返回其真实值。

如果我碰巧:

START WITH oc1.ID_ORGAO_INTELIGENCIA = S.ID_ORGAO_INTELIGENCIA

对此:

START WITH oc1.ID_ORGAO_INTELIGENCIA = 311

其中 311 是该S.ID_ORGAO_INTELIGENCIA列真正具有的值。

有没有办法让这个查询像“我认为”一样工作?

4

1 回答 1

0

为了使它起作用,我将子查询更改为另一个:

(
    select qt_.*
    from (
             SELECT QRY_NAME.*,
                    rownum as ord
             FROM (
                      SELECT oc1.ID_ORGAO_INTELIGENCIA,
                             oc1.ID_ORGAO_INTELIGENCIA_PAI,
                             connect_by_root (oc1.ID_ORGAO_INTELIGENCIA) as root
                      FROM TB_ORGAO_INTERNO oc1
                      CONNECT BY NOCYCLE PRIOR oc1.ID_ORGAO_INTELIGENCIA_PAI = oc1.ID_ORGAO_INTELIGENCIA
                  ) QRY_NAME
             WHERE root = s.ID_ORGAO_INTELIGENCIA
         ) qt_
)
于 2021-06-21T20:56:05.697 回答