.... $dbh = DBI->connect("DBI:Pg:dbname=dbname", "user", "passwd"); my $query1 = qq{select x.sum as pocetnost, sum(*) as suma from (select case when sum(*) between 1 and 9 then '01) 1-9' when sum(*) between 10 and 14 then '02) 10-14' when sum(*) between 15 and 19 then '03) 15-19' when sum(*) between 20 and 24 then '04) 20-24' when sum(*) between 25 and 29 then '05) 25-29' when sum(*) between 30 and 34 then '06) 30-34' when sum(*) between 35 and 39 then '07) 35-39' when sum(*) between 40 and 44 then '08) 40-44' when sum(*) between 45 and 49 then '09) 45-49' when sum(*) between 50 and 59 then '10) 50-59' when sum(*) between 60 and 69 then '11) 60-69' when sum(*) between 70 and 79 then '12) 70-79' when sum(*) between 80 and 89 then '13) 80-89' when sum(*) between 90 and 99 then '14) 90-99' when sum(*) between 100 and 124 then '15) 100-124' when sum(*) between 125 and 149 then '16) 125-149' when sum(*) between 150 and 174 then '17) 150-174' when sum(*) between 175 and 199 then '18) 175-199' when sum(*) between 200 and 249 then '19) 200-249' when sum(*) between 250 and 299 then '20) 250-299' when sum(*) between 300 and 499 then '21) 300-499' when sum(*) between 500 and 749 then '22) 500-749' when sum(*) between 750 and 899 then '23) 750-899' when sum(*) between 900 and 4000 then '24) viac ako 900' else '25) nezname stado' end as sum from animal as a, transfer as b where a.db_animal = b.db_animal and b.exit_dt notnull group by a.db_herd order by sum) as x group by x.sum}; my ($pocetnost, $suma); my $sth1 = $dbh->prepare($query1); $sth1->execute; my $row = $sth1->rows; $sth1->bind_col(1, \$pocetnost); $sth1->bind_col(2, \$suma); print "Number of rows: $rows"; while ($sth1->fetchrow_arrayref) { $pocetnost =~ s/....//; print RES "<TR>\n"; print RES "<TD VALIGN=TOP>$pocetnost</TD><TD VALIGN=TOP>$suma</TD>\n</TR>\n"; } print RES "</table></tbody> .......

return zero rows but through psql it works correctly with 25 rows. It is wokrs correctly through both (psql and perl) only if I skip where section and use only table animal.