I would like to know how I can get the last date of different records, that is to say that if I have a table where there will be data that is repeated then I would like to get at least the last date of one of those records, example: Suppose I have the following table where the product_id will be repeated constantly
go | product_id | Date |
---|---|---|
1 | 1 | 2021-05-07 13:53:12 |
two | 1 | 2021-05-10 09:30:43 |
3 | two | 2021-05-18 14:46:15 |
what I really want to get out of the query is the following:
product_id | Date |
---|---|
1 | 2021-05-10 09:30:43 |
two | 2021-05-18 14:46:15 |
the product with id 1 was repeated 2 times but I only want to bring the last date on which there is a record of that product and also keep the last date of the other product. I tried using max(fecha)
this but what it really did is bring the last date of the entire column, which in this case would be2021-05-18 14:46:15
Is there a function in mysql that allows me to do this? Or should I devise a case, or some condition that allows me to bring the data as I want?
You must group by
id_producto
so that it shows you the maximum (last) date of each product.