1

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

Young Al Capone
  • 369
  • 1
  • 6
  • 25

1 Answers1

0

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'));
Raphaël
  • 173
  • 11
  • Raphael you are a GENIUS!!! Can you explain me How you used CASE sentence to retrieve information in two different columns? I didn't know this technique. – Young Al Capone May 26 '17 at 18:55
  • It's basically "creating" new columns displays with conditions, the joins are already created, you're just re-arranging the rows you already have with conditions. As you can see, two of the rows have the same output, but one of them is "WHEN description LIKE 'HABER' " and the other is "description LIKE 'RETENCION' OR description LIKE 'ANTICIPO' " Which is two different columns – Raphaël May 26 '17 at 19:02
  • Thanks Raphael. I really appreciate your help! – Young Al Capone May 26 '17 at 19:34