Untitled

 avatar
unknown
sql
a year ago
2.9 kB
10
Indexable
select

    o.id_documento,
    o.numero_documento,
    o.numero_ato,

    cast(coalesce(
        (select tpa0.abreviatura from prot_ocorrencias o0
         join prot_tipos_atos tpa0 on o0.id_tipo_ato = tpa0.id_tipo_ato
         where o0.id_documento = o.id_documento and o0.numero_documento = o.numero_documento and o0.numero_ato = o.cancelado_pelo_ato_numero)
        || '.' || o.cancelado_pelo_ato_numero ||
        case coalesce(o.is_cancelamento_parcial,0)
            when 1 then '*'
            else ''
        end,
        (select
            list(tpa2.abreviatura || '.' || o2.numero_ato ||
            case coalesce(o2.is_cancelamento_parcial,0)
                when 1 then '*'
                else ''
            end)
         from prot_ocorrencias o2
         join prot_tipos_atos tpa2 on o2.id_tipo_ato = tpa2.id_tipo_ato
         where
            o2.id_documento = o.id_documento and
            o2.numero_documento = o.numero_documento and
            o2.numero_ato_cancelado = o.numero_ato)
    ) as d_varchar_2046) as cancelado_pelo_ato,

    cast(coalesce(
        (select tpa0.abreviatura from prot_ocorrencias o0
         join prot_tipos_atos tpa0 on o0.id_tipo_ato = tpa0.id_tipo_ato
         where o0.id_documento = o.id_documento and o0.numero_documento = o.numero_documento and o0.numero_ato = o.numero_ato_cancelado)
        || '.' || o.numero_ato_cancelado || ',' ||
        (select
            list(tpa3.abreviatura || '.' || o3.numero_ato)
         from prot_ocorrencias o3
         join prot_tipos_atos tpa3 on o3.id_tipo_ato = tpa3.id_tipo_ato
         where
            o3.id_documento = o.id_documento and
            o3.numero_documento = o.numero_documento and
            o3.cancelado_pelo_ato_numero = o.numero_ato),
        tpa.abreviatura || '.' || o.numero_ato_cancelado ||
            case coalesce(o.is_cancelamento_parcial,0)
                when 1 then '*'
                else ''
            end
    ) as d_varchar_2046) as atos_cancelados,

    case coalesce(o.cancelado_pelo_ato_numero,0)
        when 0 then
            coalesce((select o4.numero_ato
             from prot_ocorrencias o4
             where
                o4.id_documento = o.id_documento and
                o4.numero_documento = o.numero_documento and
                coalesce(o4.is_cancelamento_parcial,0) = 0 and
                o4.numero_ato_cancelado = o.numero_ato),0)
        else o.cancelado_pelo_ato_numero
    end as ato_cancelador_total

from prot_ocorrencias o
join prot_tipos_atos tpa
    on o.id_tipo_ato = tpa.id_tipo_ato
join prot_tipos_ocorrencias tpo
    on o.id_tipo_ocorrencia = tpo.id_tipo_ocorrencia
where
    o.id_documento = :id_documento and
    o.numero_documento = :numero_documento and
    o.numero_ato is not null
order by
    o.numero_ato
Editor is loading...