Skip to content
Advertisement

Sql Query to obtain unique values from a mixed data

I am new to MySql and have been trying to learn it for a project. I have a table as below (Table 1) and I need the output from it as Table 2.

The output is ordered by CREATED_AT field and if there are multiple rows for the same FRUIT_TYPE, the field with the least CREATED_AT value is picked.

For e..g of the 3 values for Drupes, only 1 row is taken where FRUIT is olives and the CREATED_AT values is the least.

If you put out the sql query, do explain it. 😛 Thanks in advance !

Advertisement

Answer

A simple method is a correlated subquery:

With an index on (fruit_type, created_at), this is likely to be the most performant method as well.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement