0

hola tengo un tema con este select, y no se estará soportado

es un select que anida 2 tablas en el mismo selec pero este no funciona al ejecutar el select funciona todo bien, pero al usarlo en un cursor, no reconoce la columna idresource

[42702] ERROR: column reference "idresource" is ambiguous

alguna sugerencia

        open curs_for_items for
          select grupo.ori, grupo.typeitem,grupo.description,sum(grupo.amount) amount, sum(grupo.amounttax) amounttax, max(grupo.idresource) as idresource
        from (
          SELECT
            eve.typeitem,
            eve.description,
            eve.amount,
            eve.amounttax,
            eve.idresource as idresource,
          'EVENT' ori
          FROM event eve
          WHERE idtransaction = numberTransaction
          UNION ALL
          select pr.typeitem, pr.typetax, pr.amount, pr.amounttax, pr.idresource, pr.ori
          from (
          SELECT
            evt.typeitem,
            evt.typetax,
            evt.amount,
            0 AS amounttax,
            (SELECT idresource
             FROM event
             WHERE id = evt.idevent) as idresource,
            'TAX' ori
          FROM eventtax evt
          WHERE idtransaction = numberTransaction
                AND idevent IS NOT NULL
          ) pr
          UNION ALL
          SELECT
            evnull.typeitem,
            evnull.typetax,
            evnull.amount,
            0 AS amounttax,
            (SELECT idresource
             FROM event
             WHERE id IN (SELECT a.idevent
                          FROM eventtax a
                          WHERE a.id = evnull.idtax)) as idresource,
            'TAX' ori
          FROM eventtax evnull
          WHERE idtransaction = numberTransaction
                AND idevent IS NULL
        ) grupo
        group by grupo.ori, grupo.typeitem,grupo.description,grupo.idresource
        order by grupo.idresource;
0

1 respuesta 1

0

finalmente no logre generar una solucion creo que posstgres a nivel de cursores no soporta este nivel de anidacion de querys, lo resolvi de esta menera cambiando un poco el select

        select grupo.ori, grupo.typeitem,grupo.description,sum(grupo.amount) amount, sum(grupo.amounttax) amounttax, max(grupo.idresource) idresource, count(1) cnt
        from (
          SELECT
            eve.typeitem,
            eve.description,
            eve.amount,
            eve.amounttax,
            eve.idresource,
          'EVENT' ori
          FROM event eve
          WHERE idtransaction = numberTransaction
        union all
         SELECT
            evt.typeitem,
            evt.typetax,
            evt.amount,
            0 AS amounttax,
            ev.idresource ,
            'TAX' ori
          FROM eventtax evt, event ev
          WHERE  ev.id = evt.idevent and
            evt.idtransaction = numberTransaction
                AND idevent IS NOT NULL
        UNION ALL
             SELECT
            evt.typeitem,
            evt.typetax,
            evt.amount,
            0 AS amounttax,
            ev.idresource ,
            'TAX' ori
          FROM eventtax evt, event ev, eventtax evt2
          WHERE evt2.id = evt.idtax
            and ev.id = evt2.idevent
            and evt.idtransaction = numberTransaction
            AND evt.idevent IS NULL)
        grupo
        group by grupo.ori, grupo.typeitem,grupo.description,grupo.idresource
        order by grupo.idresource;

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.