I have the following table:
mysql> select * from Hospital;
+---+-----------+------------+
| H | PR | F |
+---+-----------+------------+
| 1 | Canarias | 1984-02-01 |
| 2 | Cataluña | 1996-04-03 |
| 3 | Canarias | 2010-03-01 |
| 4 | Madrid | 1999-01-01 |
| 5 | Madrid | 2019-12-01 |
+---+-----------+------------+
5 rows in set (0.00 sec)
And I want to get the "H" in Madrid with the oldest date, so I use the following query:
mysql> select H,min(F) from Hospital where PR="Madrid" group by H;
+---+------------+
| H | min(F) |
+---+------------+
| 4 | 1999-01-01 |
| 5 | 2019-12-01 |
+---+------------+
2 rows in set (0.00 sec)
The issue is that it shows me two tuples, and I need the one with H=4, because it shows two tuples instead of 1?
With the "Describe" command the following is output:
mysql> describe Hospital;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| H | varchar(5) | NO | PRI | NULL | |
| PR | varchar(10) | NO | | NULL | |
| F | date | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
The truth is that it is a rather confusing subject. I hope I can clarify it in a simple way.
I start with the solution:
And the dataset from which I started, so that the checks can be carried out:
And now the clarifications:
F
asINDEX
and resolve toORDER BY
. In this case you have to take into account that, if two dates coincide, it will only show you the first record and not all those that coincide with that minimum date.It seems to work, but you just have to try with:
To verify that it continues to give the same result (it is usually the first one it finds), it does not matter if you look for the maximum or the minimum. The reason is because SQL doesn't work like that. A total and a projection are algebraic operations that we cannot mix. Either we calculate a total or we project, both things at the same time: no. You can search for a minimum with
MIN()
or locate which record a value corresponds to withWHERE
. But keep in mind that they are two different operations.And lastly, the
GROUP BY
. Do not form groups unless you need them. What SQL does when it finds aGROUP BY
is make separate heaps separated by the grouping fields, and compute totals for each group separately. If you are only interested in Madrid, heWHERE
is perfect. We do not need the totals of other autonomies.And never, ever, group by a primary key. By definition, you will get as many groups as there are records, and the totals of each group will coincide with the value of each record. So, with a projection, you would arrive at the same result and at a much lower computational cost. Whenever we group by a field, it must have values that are repeated in different records.
I hope this answer is useful to you. I remain at your disposal to clarify or expand any detail.
OPTION 1
Your grouping won't work because column H and column F have different values.
I would leave the query like this:
add at the end the use of
To indicate that the result obtained is limited to one record
OPTION 2
Performing a CTE within which we do:
H
,PR
andF
WHERE
where we indicate thatF
the date is equal to:PR
it is equal toMadrid
SELECT
of the columns that we indicate must returnMenor
what is the name that we gave to the * CTE *Query
Result
Or just take the query out of the CTE context and run it and it should work like this:
At the end you can get the result with a subquery:
Using MariaDB (I don't have MySQL at hand) I propose:
Which returns me: