Select extreme values from a certain range in Mysql

Asked

Viewed 57 times

0

I need to collect only the extreme values of 'id' for each given time slot. According to the data below, I need to collect the id’s 63 and 67 for the time of 00:45:22, 80 and 84 for the time of 01:15:26. How to proceed?

id   n  hora          data      AP

63  5   00:45:22    24/05/2017  3  
64  5   00:45:22    24/05/2017  1  
65  5   00:45:22    24/05/2017  3  
66  5   00:45:22    24/05/2017  3  
67  5   00:45:22    24/05/2017  3  
80  5   01:15:26    24/05/2017  5  
81  5   01:15:26    24/05/2017  5  
82  5   01:15:26    24/05/2017  5  
83  5   01:15:26    24/05/2017  5  
84  5   01:15:26    24/05/2017  5  
  • AP or N influences the expected response?

  • 1

    Yes Jefferson, I need to get a final selection that will provide me with the Aps of each id, both maximum and minimum. However, for what I asked Motta’s answer is enough

1 answer

1


MAX and GROUP BY

select hora , min(id) min_id , max(id) max_id
from tabela
group by hora 

GROUP BY is used when using aggregators as sum , count , mean, standard deviation , maximum and minimum , can also be used for eliminating duplicates , I always recommend a reading in the DBMS vendor documentation for details.

  • 1

    Motta, can you explain better what each function does? Mainly the group by. In fact, it is necessary to take the id minimum also.

  • http://www.devmedia.com.br/exemplos-com-group-by-e-com-a-clausula-having-totalizando-dados-sql-server-2008-parte-2/19839 see link for group by . The minimum was edited in the reply ,

  • 3

    Motta, the goal of the site is to generate quality content that serves as a reference for other people, not just solve a particular problem. So the ideal is that you put at least the minimum explanation of what your code does in the answer, without relying on links. Obviously it is interesting that you also cite the official reference if the person wants to extend the search.

Browser other questions tagged

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