× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



A subselect like this is a very bad idea...

better idea

with ord_ytd as (
select com_nbr, ent_nbr, coalesce(sum(opn_amt),0)
from V_ORDERS
where ord_date>='2020-01-01'
group by com_nbr, ent_nbr
), inv_ytd as (
select com_nbr, ent_nbr, coalesce(sum(shp_amt),0)
from V_INVOICES
where inv_date>='2020-01-01'
group by com_nbr, ent_nbr
), <...>
select CUS.COM_NBR, CUS.CUST_NBR, CUS.CUST_NAME, (more here),
from V_CUSTMSTR as CUS
left outer join ord_ytd CO on cus.com_nbr = CO.com_nbr and
cus.ent_nbr = CO.ent_nbr
left outer join inv_ytd CI on cus.com_nbr = CI.com_nbr and
cus.ent_nbr = CI.ent_nbr
<...>

The idea is that you build separate results sets, from the various tables
aggregated to the proper level; in this case com_nbr and ent_nbr. Then
simply join them together.

You could even build summary views (and perhaps supporting EVIs) to use in
place of the CTEs.

You might be interested in the following presentation
https://www.linkedin.com/posts/charles-wilt-2a4a303_sql-for-the-rpg-developer-activity-6641756015354355712-woZR


Charles




On Fri, Mar 6, 2020 at 9:52 AM Greg Wilburn <
gwilburn@xxxxxxxxxxxxxxxxxxxxxxx> wrote:

I am rewriting an old RPG report using SQL (and SQL views I already
have). The "report" generates a database table as output

I need one row for each customer. In each row I need columns for several
sales figures that can be found in two other views.
I have SQL views for the Customer, Invoices and Orders.

I currently have it written where each sales figure is a separate
sub-select. Note that the "where" clause for each is slightly different.

It seems like this could be written more efficiently... but I'm not sure
what approach to take

1. Use common table expressions?

2. Create the output table, then use separate SQL expressions to
insert each of the summed values?

This is a simplified version of the code (for simplicity I've replaced the
RPG variables with constant values).

SELECT CUS.COM_NBR, CUS.CUST_NBR, CUS.CUST_NAME, (more here),
(select coalesce(sum(opn_amt),0) from V_ORDERS where com_nbr=cus.com_nbr
and ent_nbr=cus.ent_nbr and
ord_date>='2020-01-01' and ord_date<='2020-03-04') as BOOK_YTD,
(select coalesce(sum(shp_amt),0) from V_INVOICES where com_nbr=cus.com_nbr
and ent_nbr=cus.ent_nbr and
inv_date>='2020-01-01' and inv_date<='2020-03-04' )as NET_YTD,
(select coalesce(sum(shp_amt),0) from V_INVOICES where com_nbr=cus.com_nbr
and ent_nbr=cus.ent_nbr and
inv_date>='2019-01-01' and inv_date<='2019-03-04' ) as
PREV_YR_TOTAL,
(select coalesce(sum(shp_amt),0) from V_INVOICES where com_nbr=cus.com_nbr
and ent_nbr=cus.ent_nbr and
inv_date>='2019-01-01' and inv_date<='2019-03-04' ) as
NET_PREV_YTD,
(select coalesce(sum(ord_amt),0) from V_ORDERSA where com_nbr=cus.com_nbr
and ent_nbr=cus.ent_nbr and
ord_date>='2019-01-01' and ord_date<='2019-03-04') as
BOOK_PREV_YTD
from V_CUSTMSTR as CUS
where CUS.COM_NBR='012';

Thanks,
Greg
--
This is the RPG programming on IBM i (RPG400-L) mailing list
To post a message email: RPG400-L@xxxxxxxxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: https://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxxxxxxxx
Before posting, please take a moment to review the archives
at https://archive.midrange.com/rpg400-l.

Please contact support@xxxxxxxxxxxx for any subscription related
questions.

Help support midrange.com by shopping at amazon.com with our affiliate
link: https://amazon.midrange.com


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

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

This mailing list archive is Copyright 1997-2024 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].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.