Group Mysql Values

Asked

Viewed 155 times

-1

Dear, I have a table with the configuration:

Tipo Medida Valor
1      0.2  5.00
1      0.4  6.00
1      0.1  5.00
1      0.3  5.00
2      0.1  3.00
2      0.2  3.00
2      0.3  5.00
2      0.4  5.00

I would like to group the values where they should contain the same type, a minimum and maximum value for this type given that the value is the same. The result would be something like:

Tipo Minimo Maximo Valor
  1   0.1     0.3  5.00
  1   0.4     0.4  6.00
  2   0.1     0.2  3.00
  2   0.3     0.4  5.00

Thank you

  • 1

    What have you tried so far? What problems have you encountered?

  • 1

    Hello, @Thiago. It is gladly that when you post a question, you also post code snippets or explain what you have tried. The way you posted the question, you’re simply outsourcing the work and that’s not what we want in the community :)

  • Okay, I understand your comment, I agree and I apologize, I will pay attention to that fact. Thank you

1 answer

1


Assuming that your table is called "table", and that your database manager supports the MIN and MAX aggregation functions (mysql and postgres support), the following query should solve your problem.

SELECT tipo, MIN(medida) AS minimo, MAX(medida) AS maximo, valor
FROM tabela
GROUP BY tipo, valor
  • Perfect, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.