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



Hi Maria

I guess I got lucky today, to get things right?

I just had a thought, something I learned from a presentation Birgitta gave.

Instead of a CTE, maybe create a view and global variables - the latter can be created with the CREATE VARIABLE statement and used in any SQL statement where host variables can be used, just you don't need the colon - so they can be in a view. You might need to create the variables before creating the view for the first time, but the variables continue to exist, I believe.

The view, then, is similar to a CTE - a CTE (or nested table expression, too) is sometimes described as a temporary view. It is still not a work table, since views are merely a SELECT statement that is run each time you use the view.

Advantage of a view is, it might simplify your SQL statements. In your RPG you can use SET statements to set the variables, instead of RPG EVALs and host variables.

Just a thought! Birgitta might jump in and help some more.

Vern

On 7/28/2020 7:52 AM, Maria Lucia Stoppa wrote:
@Vernon,
I didn't attach any example because it's really complex, but you got it
right.
As you stated, select statements can't return more than one result, so I
was wondering if it's possible to use a CET to serve multiple selects, and
it seems it's not.
Yes, the many selects that run have the same from clause and where clause
(built with host variables as you imagined), that's why I hoped I missed
some tricks or ways to do things.
Thank you

Il giorno mar 28 lug 2020 alle ore 14:34 Vernon Hamberg <
vhamberg@xxxxxxxxxxxxxxx> ha scritto:

Hi Maria

I am trying to understand what is happening - I don't know how 1 static
statement can return a COUNT(*) in one execution and the rows in another
execution - it can't be the same, since the column list isn't the same -
but maybe an example can help me.

By dynamic filters, do you mean values from the request that are used
with host variables? Performance is improved, as you probably know, with
host variables, because the optimization does not need to be done more
than once under certain conditions, as I understand it - performance
guide gives details, I think multiple use in the same RPG module is one
of those. And in many cases, the ODP for the processing is not
completely closed, so is available for other values of host variables.

I wonder, without knowing more, if any of the OLAP functions can be
used. The result set can include various subtotals in addition to the
data rows.

As to Common Table Expressions - they apply only to the statement of
which they are a part. They are recommended by the team at IBM, since
the SQL engine can optimize things very well when there are supporting
indexes and appropriate JOINs and WHERE clauses. But they are limited to
the statement - they are not available to other statements, while global
temporary tables (and temporary result table you also create, maybe in
QTEMP) are available in the job.

It does seem that if you have many SELECTs over the same temporary
result set, with different WHERE clauses (not just different values for
host variables), that a temporary table is needed. But I'm not able to
say more without seeing more of what the statements look like.

Hope this helps some!
Vern

On 7/28/2020 2:16 AM, Maria Lucia Stoppa wrote:
Hi all,
Hope you are well.

A REST API I am working on returns data split into pages: many calls are
necessary to get the complete set of data, but in this way, the user
doesn't wait too much for the first page. Data are retrieved within an
RPG
ILE service program by a static SQL statement which applies some dynamic
filters that come within the request.

Everything works fine, except the same static SQL statement is run at
least
twice to know the total number of rows (a simple count(*)) and the rows
themselves page by page.
Now, on the same data retrieved by this SQL statement, others select
statements must be run to get some totals according to different group by
clauses in order to present the data set distribution to the final user.

I hardly accepted the idea of running the same SQL statement (which, by
the way, is pretty complex) at least twice to get both the total and the
row, but I can't stand the idea of even more runs to get data
distribution.
There might be errors in my design of how the procedure should work,
nonetheless, I wonder if a single common table expression might be used
many times to serve many different select statements, as I would avoid
the
use of other solutions like global temporary tables.

Any suggestion is really appreciated.

Many thanks

--
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@xxxxxxxxxxxxxxxxxxxx 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 ...

Follow-Ups:
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.