I have two tables and I would like to left join them on an attribute, but rather than taking all matching rows in the second table I would like to choose only the entry with the highest number (latest date, etc.) in a certain column. How do I express this result in SQL?
Here's a simplified example to clarify my question.
Table `colors`
| color |
+-------+
| red |
| green |
| blue |
Table `inventory`
| color | value | shape |
+-------+-------+----------|
| red | 1 | square |
| red | 2 | circle |
| green | 7 | triangle |
Desired output:
| color | value | shape |
+-------+-------+----------|
| red | 2 | circle |
| green | 7 | triangle |
| blue | NULL | NULL |
My tables are fairly large so ideally a solution would be reasonably efficient. (There's no need for fine tuning, just trying to avoid double joins which could become huge.)