mysql - sorting via order by and row_number via user variable in mariadb -
i've read order operation done @ end of query , therefore sorting cannot forced in result before.
so i'm trying achive 1 record result duplicates using variables assign row numbers , using having-clause select first row.
sorry bad explanation, hope know mean taking @ this:
select * address left join (select address.add_nr add_nr, acc.typ typ, acc.seq seq, @num := if(@add_nr = address.add_nr, @num + 1, 1) row_number, @add_nr := address.add_nr dummy address left join ( select con_add_nr add_nr, 'xy' typ, 1 seq conctract,conctractdet con_pol_kz in('l') , con_nr=vrs_con_nr , cond_spa_nr in (102,160,173) group 1,2,3 union select ris_add_nr add_nr, 'xx' typ, 4 seq conctract,conctractdet,risaddress con_pol_kz in('p','pu','a') , con_nr=vrs_con_nr , cond_spa_nr in (102,160,173) , con_nr=ris_con_nr , ris_art='vp' group 1,2,3 union select ris_add_nr add_nr, 'yy' typ, 3 seq conctract,conctractdet,risaddress con_pol_kz in('p','pu','a') , con_nr=vrs_con_nr , cond_spa_nr in (102,160,173) , vrs_nr=ris_vrs_nr , ris_art='vp' group 1,2,3 union select ris_add_nr add_nr, 'yx' typ, 5 seq conctract,conctractdet,risaddress con_pol_kz in('p','pu','a') , con_nr=vrs_con_nr , cond_spa_nr in (102,160,173) , con_nr=ris_con_nr , ris_art='2. vn' group 1,2,3 union select ris_add_nr add_nr, 'x' typ, 2 seq conctract,conctractdet,risaddress con_pol_kz in('p','pu','a') , con_nr=vrs_con_nr , cond_spa_nr in (102,160,173) , vrs_nr=ris_vrs_nr , ris_art='2. vn' group 1,2,3 order add_nr,seq asc) acc on acc.add_nr=address.add_nr having row_number=1) acc on address.add_nr=acc.add_nr;
so problem order operation done @ end when row_number has been assumed. need sort result sequence number "seq" first, achieving first entry gets row_number=1. @ moment first entry in "unsorted" result gets row_number=1 , not need.
don't use having
columns you're generating on fly user variable. mysql performs optimizations mess up. instead, use where
clause in outer query.
select * address left join (select address.add_nr add_nr, acc.typ typ, acc.seq seq, @num := if(@add_nr = address.add_nr, @num + 1, 1) row_number, @add_nr := address.add_nr dummy address cross join (select @add_nr := 0, @num := 0) init left join ( select con_add_nr add_nr, 'xy' typ, 1 seq conctract,conctractdet con_pol_kz in('l') , con_nr=vrs_con_nr , cond_spa_nr in (102,160,173) group 1,2,3 union select ris_add_nr add_nr, 'xx' typ, 4 seq conctract,conctractdet,risaddress con_pol_kz in('p','pu','a') , con_nr=vrs_con_nr , cond_spa_nr in (102,160,173) , con_nr=ris_con_nr , ris_art='vp' group 1,2,3 union select ris_add_nr add_nr, 'yy' typ, 3 seq conctract,conctractdet,risaddress con_pol_kz in('p','pu','a') , con_nr=vrs_con_nr , cond_spa_nr in (102,160,173) , vrs_nr=ris_vrs_nr , ris_art='vp' group 1,2,3 union select ris_add_nr add_nr, 'yx' typ, 5 seq conctract,conctractdet,risaddress con_pol_kz in('p','pu','a') , con_nr=vrs_con_nr , cond_spa_nr in (102,160,173) , con_nr=ris_con_nr , ris_art='2. vn' group 1,2,3 union select ris_add_nr add_nr, 'x' typ, 2 seq conctract,conctractdet,risaddress con_pol_kz in('p','pu','a') , con_nr=vrs_con_nr , cond_spa_nr in (102,160,173) , vrs_nr=ris_vrs_nr , ris_art='2. vn' group 1,2,3 order add_nr,seq asc) acc on acc.add_nr=address.add_nr) acc on address.add_nr=acc.add_nr row_number = 1;
this optimization briefly alluded in documentation
having
mergedwhere
if not usegroup by
or aggregate functions (count()
,min()
, , on).
what means if can treat having
clause where
, so, remove rows results while generating result set, instead of waiting until result rows produced.
Comments
Post a Comment