To go a bit further - and not to say it'll make it perform better! Maybe look a little better, cleaner.

In the "order by" he can use "lineSeq" instead of the formula - order by is done on the result set.

"Group by" has to use the fomula, IIRC.

It is unfortunate that he has to substring something to make a comparison - it is possible this will result in a table scan, no matter what.

Indexes (logical files with keys) are critical for performance - the OP will want to run whatever statement through Visual Explain in Operations Navigator to see just what the optimizer is doing.

HTH
Vern

On 10/15/2015 3:46 PM, Alan Shore wrote:
Hi Art
You say - I need help with improving/simplifying this SQL statement:
What exactly are you trying to achieve?
Better readability?
Quicker response?

What may impriove the response (and I am really stressing MAY) is to use a Common Table Expression (CTE)
For example

New With extractf as
New (select * from dbslsdlg where dhdate >= 20150701 and dhdate <= 20150730)
SELECT substr(idept,1,1) as lineSeq, dpdesc, 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
Removed
changed from extractf left join KCX340D.dbprodpf on dhino = ino
Left Join DBSLSDXL1 b on aINo = ino
Left join dbdeptpf on substr(idept,1,1) = deptno
where
aDate >= 20150701 and aDate <= 20150730
group by substr(idept,1,1), dpdesc order by 1, dpdesc;

I believe (but don't quote me on this) the joins are only done on the records you are interested in and not the entire file

Couple of things - hopefully someone will respond saying that my thoughts are correct
Hopefully, this is what you are looking for


Alan Shore
E-mail : ASHORE@xxxxxxxx
Phone [O] : (631) 200-5019
Phone [C] : (631) 880-8640
'If you're going through hell, keep going.'
Winston Churchill

-----Original Message-----
From: MIDRANGE-L [mailto:midrange-l-bounces@xxxxxxxxxxxx] On Behalf Of Art Duarte
Sent: Thursday, October 15, 2015 4:29 PM
To: Midrange Systems Technical Discussion
Subject: SQL Help

Hello,

I need help with improving/simplifying this SQL statement:

SELECT substr(idept,1,1) as lineSeq, dpdesc, 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
Left join dbdeptpf on substr(idept,1,1) = deptno
where dhdate >= 20150701 and dhdate <= 20150730
AND aDate >= 20150701 and aDate <= 20150730
group by substr(idept,1,1), dpdesc order by 1, dpdesc;

Appreciate your help!

Thank you
Art Duarte

--
This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing list To post a message email: MIDRANGE-L@xxxxxxxxxxxx To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/midrange-l
or email: MIDRANGE-L-request@xxxxxxxxxxxx Before posting, please take a moment to review the archives at http://archive.midrange.com/midrange-l.



This thread ...

Follow-Ups:
Replies:

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].