SELECT calldate,src,substring(dst from '1#\"%#\"' for '#') as dst, CASE billsec % 6 WHEN 0 THEN ((( (billsec/6) *6)::float(1)/60)::float(1))::float(1) ELSE ((( ((billsec/6 +1)* 6))::float(1)/60)::float(1))::float(1) END as minutes,accountcode,(select rate from rates where lata = accountcode) as RATE, CASE billsec % 6 WHEN 0 THEN ((( (billsec/6) *6)::float(1)/60)::float(1) * (select rate from rates where lata = accountcode) )::float(1) ELSE ((( ((billsec/6 +1)* 6))::float(1)/60)::float(1)* (select rate from rates where lata = accountcode) )::float(1) END as amount FROM public.cdr WHERE calldate >= '11/01/2004' and calldate < '12/01/2004' and (src like '$src%') and accountcode IN ('INTRA','INTER','ONNET') and lastdata <> '' and billsec > 0 and billsec < 43200 order by calldate