mysql - fill groups with values based on group values (update with group bys) - SQL - postgre -


i have situation in produce output, , based on gorup of belonging, gets , index (common group). production goes on , insert new values belong same group. want assign these new records same "marker" group had. i'd sql alone. detailed in picture below, groups composed tuple (continent, zone, state) i, in fact, group by. (table name: 'geo')

enter image description here

how can put number 5 aside manchester , 6 aside lyon?

if ok first inserting data, , running second query update blank group numbers, there 1 option:

update yourtable t1 set group = t2.group (     select continent, zone, state            max(group) group     yourtable     group continent, zone, state ) t2 t1.continent = t2.continent ,       t1.zone      = t2.zone      ,       t1.state     = t2.state 

the inner query in from clause finds maximum group value each continent, zone, state combination. there should 2 types of values each group combination, namely number and/or null. null value indicate record newly inserted, ignored max function.


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -