I don't see anything wrong with your proposed method. Sub-select within a result set if fine. I would NOT go with your option #2, which is build a file and then update it. That would be a performance negative. Also, you don't list it as a possibility, but, writing the results to a file, probably to be reporting on by another program might be considered a performance negative, if you're just going to read that table to report. Why not have that report read directly from the file and forgo the outfile, but, I realize that there may be other factors that make this unfavorable.
-----Original Message-----
From: RPG400-L <rpg400-l-bounces@xxxxxxxxxxxxxxxxxx> On Behalf Of Greg Wilburn
Sent: Friday, March 6, 2020 11:52 AM
To: RPG programming on IBM i <rpg400-l@xxxxxxxxxxxxxxxxxx>
Subject: Most efficient SQL approach
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.
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.