-3

I need to skip results with high price per day. I've got a table like this:

+------+-------------+-------+
| days | return_date | value |
+------+-------------+-------+
| 2    | 2017-12-27  | 15180 |
| 3    | 2017-12-28  | 14449 |
| 4    | 2017-12-29  | 13081 |
| 5    | 2017-12-30  | 11203 |
| 6    | 2017-12-31  |  9497 |
| 6    | 2017-12-31  |  9442 |
+------+-------------+-------+

How can I print only the lowest price for 6 days (9442 in this example).

1 Answers1

0

We can use a GROUP BY clause and an aggregate function. For example:

SELECT t.days
     , t.return_date
     , MIN(t.value) AS min_value 
  FROM mytable t
 GROUP
    BY t.days 
     , t.return_date

This doesn't really "skip" rows. It accesses all the rows that satisfy the conditions in the WHERE clause (in this example, every row in the table). Then MySQL collapses rows into groups (in this example, rows with identical values of days and return_date get put into a group. The MIN(t.value) aggregate function selects out the minimum (lowest) value out of the group.

The query above is just an example of one approach of satisfying a particular specification.

spencer7593
  • 106,611
  • 15
  • 112
  • 140