Tuesday 15 July 2014

sql - MySQL GROUP BY with MIN - incorrect column data -


I have seen here: and SO on the same page but I do not seem to put all my areas in line properly .

I think I am in the hip to find it, but maybe I'm going on the wrong path and need to look at it differently.

The unit I want is the lowest fare per sleeping count, which sets the merge flag to 1.

My SQL bilal:

merge = 1 with query result all Rental units

The above image was obtained from this query:

  SELECT ru. AS, Zone, rental_unit_names, run on run.id = ru.name_id, run.merge = 1 command by run.merge = 1 command ASC by order, run.name, ru.rent, ru.bedrooms , Ru.bedrooms ASC, ru.rent ASC   

Rent units with merge = 1 Property name and value grouped by minimum bedroom query result

The image above is the result of this query:

  SELECT ru.id, run.name, Ru.rent, MIN (ru.rent) as min_rent, to rent from ru.bedrooms AS ru JOIN rental_unit_names running on AS run .id = Ru.name_id WHERE run.merge = 1 GROUP by ru.name_id, ru.bedrooms ORDER run.name asc, ru.bedrooms asc, ru.rent asc, ru.id asc   

For the most part, all look fine and banded until you see on line 4. Rental prices do not line up and id is not 6 not 5 .

The image below is my desired result.

desired result

Edit :: 1 ::

Do I need to create a linking table with 2 columns on which the rental unit ID is in one column and in the other column the rental unit name id? Or at least do this as a derived table?

Generally, unless you're trying to perform some kind of MySQL "magic" you Always group every in your SELECT list by non-stationary columns.

In your case, get a list of the best opinion (name, # bedroom, minimum fare), and then find all the rows that match these values ​​- in other words, all the rows (Name, # bedroom, rent) list matches the minimum fare: SELECT ru.id, run.name, ru.rent, ru.bedrooms Move to rental_unit_names run.id = ru.name_id where run.marg = 1 and (run.name, ru.bedrooms, ru.rent) IN (SELECT inrun.name, inru.bedrooms, MIN (inru.rent) rental_unit Ren_unit_names to rent from s inrun.id = inru.name_id by inrun.merge = 1 group by inran.name, in.add.badroom)

this query all will give minimum-hire units by name / bedroom. Some places of sample data are related to the following. To include only one of the "tied" rows (Anyone with the lowest rental_units.id , try it instead - there is only one change MIN (ru.id) < / Code> In addition to an overall GROUP BY on the first line and last line:

  SELECT MIN (ru.id) AS ru_id, run.name, ru.rent , Ren__inits ru zone rental_unit_names from run.id = ru.name_id from ru.bedrooms where run.maj = 1 and (run.name, ru.bedrooms, ru.rent) (SELECT inrun.name, inru.bedrooms, MIN (Inru.rent) Rent ren_unit_nights from ren_units inrun.id = inru.name_id Add to inrun WHERE inrun.merge = 1 Group D By run.name by inrun.name, inru.bedrooms) Group, ru.rent, ru.bedrooms    

No comments:

Post a Comment