Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
¿Cómo obtener valor de una columna que tiene un recuento máximo en postgres?

Entiendo que la función MAX devuelve un valor máximo de una columna determinada para una columna numérica. Quiero devolver el recuento máximo de una columna. La siguiente tabla tiene dos columnas fabricante, producto.

 maker product A Printer B Laptop B Laptop A Printer A Monitor A Printer A Scanner

Pero cuando ejecuto la consulta '''select maker, max(type) from table group by maker; ''' Obtengo el resultado como

 maker product A Monitor B Laptop

Quiero qué producto tiene un recuento máximo con respecto al fabricante como se muestra a continuación.

 maker product A Printer B Laptop

Nota: Como el recuento de fabricantes es diferente, no podemos establecer un número común en la cláusula Tener.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Puede hacerlo con la función de ventana RANK() :

 select t.maker, t.product from ( select maker, product, rank() over (partition by maker order by count(*) desc) rn from tablename group by maker, product ) t where t.rn = 1

Ver la demostración .
Resultados:

 | maker | product | | ----- | ------- | | A | Printer | | B | Laptop |
over 4 years ago · Santiago Trujillo Report

0

En PostgreSQL, la solución más simple es usar DISTINCT ON :

 SELECT DISTINCT ON(maker) maker, product FROM atable GROUP BY maker, product ORDER BY maker, count(*) DESC;
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!