Wednesday, 15 September 2010

mysql - Extract Values from records with max/min values -



mysql - Extract Values from records with max/min values -

i posted question before , got great reply realize went logic wrong way. given table this:

name|valuea|valueb1|valueb2 bob | 1| 200| 205 bob | 2| 500| 625 bob | 7| 450| 850 bob | 3| 644| 125 ann | 4| 120| 120 ann | 8| 451| 191 ann | 9| 145| 982

i trying max/min values each unique names , ended with

create tablea (select name,max(valuea),min(valuea),max(valueb1,max(valueb2) grouping name)

but gave me (naturally) high/low each of a, b1, b2 e.g.

bob|1|7|200|644|205|850

what looking b1 , b2 values each of lowest , highest values per unique name in other words in above need

bob|1|7|200|205|450|850 ann|4|9|120|120|145|982

which gives me high , low values , b1 , b2 values contained in high , low value records.

(this not duplicate question. lastly question asked , answered how pull high , low values 3 different fields given unique name new table. turns out not needed although first question answered (and marked such). need values 2 fields high , low values of field given name. if @ question see , solutions in fact different)

select tmin.name, tmin.valuea, tmax.valuea, tmin.valueb1, tmin.valueb2, tmax.valueb1, tmax.valueb2 ( select name, max(valuea) valueamax, min(valuea) valueamin `foo` grouping name ) t bring together `foo` tmin on t.name = tmin.name , t.valueamin = tmin.valuea bring together `foo` tmax on t.name = tmax.name , t.valueamax = tmax.valuea;

mysql max greatest-n-per-group min create-table

No comments:

Post a Comment