I have looked here: Selecting all corresponding fields using MAX and GROUP BY and similar pages on SO but I cannot seem to get all my fields to line up properly.
I feel like I'm at the cusp of figuring this out but maybe I'm heading down the wrong path and need to look at this differently.
What I want is the unit with the lowest rent per property name per bedroom count that have the merge flag set to 1.
My SQL Fiddle: http://sqlfiddle.com/#!2/881c41/2
The image above was obtained with this query:
SELECT ru.id, run.name, ru.rent, ru.bedrooms
FROM rental_units AS ru
JOIN rental_unit_names AS run
on run.id = ru.name_id
WHERE run.merge = 1
ORDER BY run.name ASC, ru.bedrooms ASC, ru.rent ASC
The image above is the result of this query:
SELECT ru.id, run.name, ru.rent, MIN(ru.rent) AS min_rent, ru.bedrooms
FROM rental_units AS ru
JOIN rental_unit_names AS run
on run.id = ru.name_id
WHERE run.merge = 1
GROUP BY ru.name_id, ru.bedrooms
ORDER BY run.name ASC, ru.bedrooms ASC, ru.rent ASC, ru.id ASC
For the most part all looks fine and dandy until you look at row 4. The rent values do not line up and the id
should be 6 not 5.
The image below is my desired result.
:: EDIT 1 ::
Do I need to create a linking table with 2 columns that has the rental unit id in one column and the rental unit name id in the other column? Or at least do this as a derived table somehow?