Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

PORBLEMA CON PIVOT ANIDADO

Javier RinconFeb 26 2016 — edited Feb 26 2016

Buen día a todos, primero quiero informarles que este es mi primer post o pregunta en foros de oracle, sin embargo espero que todas las recomendaciones que me puedan hacer respecto a como debo hacer las preguntas de forma correcta, seran bien recibidas.

Bien como lo menciono en el titulo estoy haciendo un pivot anidado de una misma tabla, en donde necesito saber la cantidad (1 hora, 1 dia, un porcentaje %) y el valor pagado bajo un determinado criterio ( de 1 a n criterios) para cada uno de los empleados.

DATOS EJEMPLO

documento_empleadocriteriocantidadvalor
1010101110200
10101015151000

Sin embargo para solo algunos criterios necesito la cantidad y el valor, y para otros solo el valor. Por ese motivo decidí efectuar el siguiente query que se ejecuta sin ningún error. 

QUERY

SELECT h1.*,

              h2.*

FROM (

      SELECT *

        FROM (

        (SELECT documento_empleado,

                       criterio,

                       valor,

                       cantidad

            FROM tabla_pagos

          WHERE documento_empleado = 1010101

               AND empresa = 'EMPRESA 1'

               AND proceso = 1

               AND radicacion = 515253

               AND trunc(fecha) >= trunc(SYSDATE-60)

          )

          pivot (sum(valor) val, sum(cantidad) cant  FOR criterio IN (1 AS "SUELDO"))

          )

         )h1,

        ( SELECT *

            FROM (

            (SELECT documento_empleado,

                           criterio,

                           valor

               FROM tabla_pagos

            WHERE documento_empleado = 101010 AND

                 AND empresa = 'EMPRESA 1'

                 AND proceso = 1

                 AND radicacion = 515253

                 AND trunc(fecha) >= trunc(SYSDATE-60)

          ORDER BY documento_empleado desc

          )

        pivot (SUM(valor) FOR criterio IN (51 AS "DESCUENTO PENSION"))

        )

       ) h2

WHERE h1.documento_empleado = h2.documento_empleado;

Pero tengo el problema que la columna documento_empleado se repite, y no se como hacer un except de esa columna, teniendo en cuenta que debo hacer la relación (WHERE) para que los datos sean consistentes. ¿Alguna idea de como puedo hacer este query correctamente?.

Agradezco a quien me pueda ayudar.

*** Moderator action (Timo): User, this is an English language forum. Please Post your question in English language. Translation by Google follows ***

TRANSLATION:

Good day to all , first I want to inform you that this is my first post or ask in forums oracle , however I hope that all the recommendations that can make me regarding how I do the questions correctly, will be well received.

Well as I mentioned in the title I'm doing a nested one pivot table , where I need to know the amount (1 hour, 1 day , a percentage %) and the amount paid under a certain criterion ( 1 to n criteria) for each one of the employees .

DATOS EJEMPLO

documento_empleadocriteriocantidadvalor
1010101110200
10101015151000

However for some criteria I need only the quantity and value , and for others only the value. For this reason I decided to make the following query that runs without any errors.

QUERY

SELECT h1.*,

              h2.*

FROM (

      SELECT *

        FROM (

        (SELECT documento_empleado,

                       criterio,

                       valor,

                       cantidad

            FROM tabla_pagos

          WHERE documento_empleado = 1010101

               AND empresa = 'EMPRESA 1'

               AND proceso = 1

               AND radicacion = 515253

               AND trunc(fecha) >= trunc(SYSDATE-60)

          )

          pivot (sum(valor) val, sum(cantidad) cant  FOR criterio IN (1 AS "SUELDO"))

          )

         )h1,

        ( SELECT *

            FROM (

            (SELECT documento_empleado,

                           criterio,

                           valor

               FROM tabla_pagos

            WHERE documento_empleado = 101010 AND

                 AND empresa = 'EMPRESA 1'

                 AND proceso = 1

                 AND radicacion = 515253

                 AND trunc(fecha) >= trunc(SYSDATE-60)

          ORDER BY documento_empleado desc

          )

        pivot (SUM(valor) FOR criterio IN (51 AS "DESCUENTO PENSION"))

        )

       ) h2

WHERE h1.documento_empleado = h2.documento_empleado;


But I have the problem that the documento_empleado column is repeated , and not how to make a column except that , considering that I do the relationship ( WHERE) so that the data is consistent . Any idea how I can do this query correctly ?.

Thank who can help me .

This post has been answered by Etbin on Feb 26 2016
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 25 2016
Added on Feb 26 2016
7 comments
2,560 views