sqloracle-databasejoininline-view

Oracle SQL How can I separate values from a column in two different columns?


I want to code a query to return description of some concepts and their respective price but I want to make two different columns to categorise two diferentes items categories of values. Is it possible?

SELECT b.descripcion CONCEPTO, a.cantidad, a.importe,
       c.descripcion
FROM   detalles_liquidaciones a
JOIN   conceptos b
ON    (a.codigo_concepto = b.codigo)
JOIN   tipos_conceptos c
ON    (b.codigo_tipo = c.codigo)
WHERE  a.numero_liquidacion = 13802
AND    c.descripcion IN ('HABER', 'RETENCION', 'ANTICIPO');

Output Query

I want to code something like this: Ideal query


Solution

  • Could this work ? ( Perhaps there's a better solution, it feels a bit tricky... litterally "switching" ):

    SELECT Concepto ,
          (CASE
             WHEN description LIKE 'HABER' THEN
              importe
             ELSE
              NULL
           END) haberes,
           (CASE
             WHEN description LIKE 'HABER' THEN
              cantidad
             ELSE
              NULL
           END) cantidad,
           (CASE
             WHEN description LIKE 'RETENCION' OR description LIKE 'ANTICIPO' THEN
              importe
             ELSE
              NULL
           END) retenciones
      FROM (SELECT b.descripcion concepto, a.cantidad, a.importe, c.descripcion
              FROM detalles_liquidaciones a
              JOIN conceptos b
                ON (a.codigo_concepto = b.codigo)
              JOIN tipos_conceptos c
                ON (b.codigo_tipo = c.codigo)
             WHERE a.numero_liquidacion = 13802
               AND c.descripcion IN ('HABER', 'RETENCION', 'ANTICIPO'));