# your original queries format odd so I adjusted it slightly # think this may also be the source of your results # being duplicated my $sth = $dbh->prepare_cached(qq!SELECT DISTINCT A.p_code, A.business, A.status, A.value, A.login, A.enc_date, B.name, B.state FROM business A, users B WHERE A.login = ? AND A.data_enc_proposta = ? ORDER BY A.login, A.data_enc_proposta!); } $dbh = SysUtils::Connect; # $login_value and $data_enc_value have to come # from somewhere. $sth->execute($login_value,$data_enc_value); while (my ($rs,$neg,$st,$vp,$gel,$dte,$gen,$fil) = $sth->fetchrow_array()) { my $hashref = { $fil => { $gel => { $dte => { cliente => "$rs", negocio => "$neg", status => "$st", valor => "$vp", nome => "$gen" } } } } ); ListaDados($hashref); }