How to group this type of query?

3

I have problems with formulating the query to resolve the following. I have a table like for example (illustration). I need to group by the trio of columns that appear in yellow and the fourth column of the result of the query would be the record that appears first.

... and I need to see the result in the following way ...

    
asked by jeissoni22 14.10.2016 в 18:24
source

3 answers

4

When using DISTINCT ON , the query takes the first:

SELECT DISTINCT ON ("NIT", "FACTURA", "PACIENTE")
       "NIT", "FACTURA", "PACIENTE", "CONCEPTO"
  FROM tabla;

Example on rextester.com

    
answered by 14.10.2016 / 18:43
source
0

If it's how you explain it in the image, since you do not give more data, it would be something like:

SELECT nit,factura,cliente FROM tu_tabla WHERE concepto = 'AAA'
    
answered by 14.10.2016 в 18:30
0
WITH TablaAgrupada AS 
(
SELECT  
      TABLA.NIT, TABLA.FACTURA, TABLA.PACIENTE,
      TABLA.CONCEPTO,
      ROW_NUMBER() OVER(PARTITION BY TABLA.NIT, TABLA.FACTURA, TABLA.PACIENTE ORDER BY TABLA.NIT ASC) AS RowNumber
FROM
    TABLA 
GROUP BY
    TABLA.NIT, TABLA.FACTURA, TABLA.PACIENTE, TABLA.CONCEPTO
)
SELECT  NIT,FACTURA,PACIENTE,CONCEPTO
FROM    TablaAgrupada
WHERE   RowNumber = 1

Try this way, any questions or suggestions comments. Slds ...

    
answered by 15.10.2016 в 20:33