I do not see any advantage with CTE, View, etc. They seem to complicate
things unnecessarily without adding any benefit in this case. I advise our
programmers to do calculation first and then join with master tables in
last step. This reduces amount of memory required for sort and will help
when millions of records are involved. See the following SQL to get an idea
about what I am suggesting.


Also, you might want to follow Vern's advice (using Visual Explain) and
check any new indexes are needed. As explained by Vern, indexes are
critical for performance.

SELECT a.lineSeq,
dpdesc,
a.location,
a.Sales_Amt,
a.Cost_Amt,
a.Margin
from
(
SELECT substr(idept,1,1) as lineSeq,
min(dhloc) as location,
sum(dhsell*dhqty) + Sum(aSell * aQty) as Sales_Amt,
sum(dhcost*dhqty) + Sum(aCost * aQty) as Cost_Amt,
(cast( ( sum(dhsell*dhqty) + Sum(aSell * aQty) ) - (sum(dhcost*dhqty) +
Sum(aCost * aQty)) as double) / cast(sum(dhcost*dhqty) + Sum(aCost * aQty)
as double)) as Margin
from dbslsdlg left join KCX340D.dbprodpf on dhino = ino
Left Join DBSLSDXL1 b on aINo = ino
where dhdate >= 20150701 and dhdate <= 20150730
AND aDate >= 20150701 and aDate <= 20150730
group by substr(idept,1,1)
) a
Left join dbdeptpf on a.lineseq = deptno
order by 1;

This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2019 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].