Subject Re: [firebird-support] Firebird migration to increase perfomances
Author Svein Erling Tysvær
>Yes I just checked my original query will return 6 rows without the distinct

OK, then lets add another CTE so that the calculations aren't multiplied:

with tmp1(surname, name, trxdate, source, job_number, creditnr, sourceid, serviceadvisOR) as
(select distinct trim(c.name||' '||c.surname), trim(ca.name), j.jdate, j.source, i.job_number, j.source2, j.sourceid, s.name||' '||s.surname
from journal j
join invoices i on j.source = i.inv_nr
left join customer c on j.cid = c.cid
left join cashcustomer ca on j.source = ca.invnr
and i.job_number = ca.jobnr
left join staff s on j.serviceadvisOR = s.staff_id
where j.source not starting 'STOCK UNIT'
and j.jdate between '2015/06/01' AND '2015/06/22'
and j.source not starting 'DEPOSIT'
and j.cid <> 0
and j.module <> 2
and ((j.source2 not starting 'S/D'
and j.source2 not starting 'P/D')
or j.source2 is null)
and j.description not starting 'SPLIT INV'
and j.jtype in (1, 4)),
tmp2(surname, name, trxdate, source, job_number, creditnr, sourceid, serviceadvisOR, credVAT, VAT,invoicetotalINC, credinc, creditnote,
invoiceINCRedo, invoiceVATRedo, cost) as
(select t.surname, t.name, t.jdate, t.source, t.job_number, t.creditnr, t.sourceid, t.serviceadvisOR
sum(iif(j.accnr = '5995300' and j.module in (66, 77) and t.source2 = j.source2 and j.jtype = 1, coalesce(j.creditamount, 0) - coalesce(j.debitamount, 0), 0)),
sum(iif(j.accnr = '5995300' and j.jtype = 1, coalesce(j.creditamount, 0) - coalesce(j.debitamount, 0), 0)),
sum(iif(j.accnr = '3993100' and j.jtype = 1, j.debitamount, 0)),
sum(iif(j.accnr = '1501500' and j.jtype = 1, coalesce(j.debitamount, 0)-coalesce(j.creditamount, 0), 0)),
sum(iif(j.accnr = '1501500' and j.jtype = 4, coalesce(j.creditamount, 0)-coalesce(j.debitamount, 0), 0)),
sum(iif(j.accnr = '3993100' and j.jtype = 1 and j.module = 77, j.creditamount, 0)),
sum(iif(j.accnr = '5995300' and j.jtype = 1 and j.module = 77, coalesce(j.debitamount, 0)-coalesce(j.creditamount, 0), 0)),
sum(iif(j.accnr starting '6' and j.jtype = 1, coalesce(j.debitamount, 0)-coalesce(j.creditamount, 0), 0))
from tmp1 t
join journal j on t.source = j.source
and t.trxdate = j.jdate
and j.jtype in (1, 4)
and (j.accnr in ('1501500', '3993100', '5995300')
or j.accnr starting '6')
group by 1, 2, 3, 4, 5, 6, 7, 8)
select surname, name, trxdate, source, job_number, creditnr, sourceid, serviceadvisOR, credVAT, VAT, invoicetotalINC, invoicetotalINC-VAT+credvat as invoicetotal,
credinc+creditnote-invoiceINCRedo+invoiceVATRedo credits, invoicetotalINC-VAT+credinc+creditnote-invoiceINCRedo as nett, cost,
invoicetotalINC-VAT+credinc+creditnote-cost-invoiceINCRedo as gp,
invoicetotalINC-VAT+credinc+creditnote-cost-invoiceINCRedo/NULLIF(invoicetotalINC-VAT+credinc+creditnote, 0)*100 as gppersent
from tmp
where invoiceINC-invoicevat+credvat <> 0
order by 1,3,4

Does this get you the right result?

By the way, I'm positively surprised if the performance improved from almost 16 seconds to 0.16 seconds, I'd hoped for up to a 10-fold improvement, not 100-fold. I'm sure you didn't mean 0.16 milliseconds, Firebird is simply not that quick.

HTH,
Set