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



I hope this clears things up a bit.

I'm trying to use a series of SQL UDF counters that I've created. One of
them is an MTD() function that will return running, month-to-date totals
when given a date, a key and an amount. The idea is that the following SQL
would produced the following result:

SELECT DTE, KEY, AMT, MTD(AMT)
FROM MYTABLE
ORDER BY KEY, DTE

01/15/2010 AAAAA 12 12
01/20/2010 AAAAA 2 14
01/31/2010 AAAAA 3 17
02/15/2010 AAAAA 5 5
02/15/2010 BBBBB 3 3
02/20/2010 BBBBB 3 6

I hope that's enough example to make it clear. The issue I have is that SQL
does not present its data to the function in the ORDER_BY sequence, but
rather in some indeterminate sequence. So the resultant running totals
don't make sense even though the data is FINALLY PRESENTED in the correct
sequence.

A suggestion (CRPENCE, I believe) was to do something like this:

WITH T1 AS (
SELECT DTE, KEY, AMT
FROM MYTABLE
ORDER BY KEY, DTE
)
SELECT DTE, KEY, AMT, MTD(AMT)
FROM T1

From what I understood, that poster was saying that the T1 CTE's result
would always be processed in arrival sequence, thereby causing MTD to
receive the data in the necessary sequence, and bringing order to the chaos
that was seen before.

As I posted (implied) previously, the result of the SELECT .. FROM <CTE> is
not presenting the data in that sequence at all.

My original goal was to find a way to cause SQL to PROCESS the data in a
particular sequence such that it's actually possible to have a running
totals function.

I wrote code to test various scenarios, which I placed at
http://code.midrange.com/478dd3badf.html. It's quite simple and not very
useful except to prove this concept. When I execute that code like this:

SELECT ITMCOUNT('AAAAA') FETCH FIRST 4 rows only

My result is a sequential count starting from 1. Now if I do

SELECT ITMCOUNT('BBBBB') FETCH FIRST 4 rows only

My result is again a sequential count starting from 1. Continuing on,

SELECT ITMCOUNT('BBBBB') FETCH FIRST 3 rows only

One number in the sequence (5) is skipped, and I get the next three in the
sequence. So the function works, even if FETCH FIRST x does some flaky
stuff. This isn'at about FETCH FIRST.

But when I run this function using the following SQL, the results are off
the charts:

WITH T1 AS (SELECT KEY FROM MYTABLE
GROUP BY KEY
ORDER BY KEY)
SELECT KEY, ITMCOUNT(KEY) FROM T1

This is equally flaky, and not exactly the same:

WITH T1 AS (SELECT KEY FROM MYTABLE
GROUPU BY KEY
ORDER BY KEY)
SELECT KEY, ITMCOUNT(KEY) FROM T1 ORDER BY KEY

This is what I need to solve.

I hope that makes it clearer.


Dennis Lovelady
http://www.linkedin.com/in/dennislovelady
--
"No one is useless in this world who lightens the burdens of another."
-- Charles Dickens

Dennis,

Haven't been following this thread closely, but if you want to order
your
output you need to specify your ORDER BY after the "select dept, name
from
t1".

Regards,

Luis Rodriguez
IBM Certified Systems Expert ? eServer i5 iSeries


On Wed, Jun 16, 2010 at 11:40 AM, Dennis Lovelady
<iseries@xxxxxxxxxxxx>wrote:

Dang it, I'm back to square 1 on this.

This simple select:

with t1 as (
select dept, name
from names
group by dept, name
order by dept, name
)
select dept, name from t1

Produced these results at V5R3:

S&C TEMPLETON
EXEC DALY
IS KING
STAFF NESSON
OPER PINA
OPER GYAMFI
IS GOLBA

Is there really no way to tell SQL the order in which rows are to be
processed?

Dennis Lovelady
http://www.linkedin.com/in/dennislovelady
--
"I knew I was an unwanted baby when I saw that my bath toys were a
toaster
and a radio."
-- Joan Rivers


I used to think CASE tools were oh so cool. Then RPG actually
started
getting improvements. And the CASE tool vendors thought
maintenance
money
was reserved for paying off leveraged buy outs instead of product
improvements and they lagged behind, further and further, until
they
fell
into disuse.

Now I suppose SEQUEL is going that same route.


Rob Berendt
--
Group Dekko Services, LLC
Dept 01.073
Dock 108
6928N 400E
Kendallville, IN 46755
http://www.dekko.com





From: "Dennis Lovelady" <iseries@xxxxxxxxxxxx>
To: "'Midrange Systems Technical Discussion'"
<midrange-l@xxxxxxxxxxxx>
Date: 06/16/2010 05:10 AM
Subject: RE: SQL row processing order for UDF
Sent by: midrange-l-bounces@xxxxxxxxxxxx



Thanks to all who responded. Rob's idea seems to fit the bill best
in
this
case.

Dennis Lovelady
http://www.linkedin.com/in/dennislovelady
--
"Our vision is to speed up time, eventually eliminating it."
-- Alex Schure

Create a view with that statement. Access the view with SEQUEL?


Rob Berendt


Dennis
I didn't, but that's a very good idea.

However, it's not a complete solution since some uses of this,
against
my
fervent complaints, will be implemented using the SEQUEL product,
which
has
no support for the WITH statement (among many other things).
Don't
get
me
started.

Any response to the "is it expected behavior" part?

Dennis Lovelady
http://www.linkedin.com/in/dennislovelady
--
Time is nature's way of keeping everything from happening at once.


did you try to use an CTE?

With x as (Select TRNADAT, Amount
From MyTable
Where ...
Order By TRNDAT)
Select x.* MTD(Amount)
From x
;

Mit freundlichen Grüßen / Best regards

Birgitta Hauser

"Shoot for the moon, even if you miss, you'll land among the
stars."
(Les
Brown)
"If you think education is expensive, try ignorance." (Derek
Bok)
"What is worse than training your staff and losing them? Not
training
them
and keeping them!"

-----Ursprüngliche Nachricht-----
Von: midrange-l-bounces@xxxxxxxxxxxx
[mailto:midrange-l-bounces@xxxxxxxxxxxx] Im Auftrag von Dennis
Lovelady
Gesendet: Tuesday, 15. June 2010 15:30
An: 'Midrange Systems Technical Discussion'
Betreff: SQL row processing order for UDF

I created a UDF like a more complex version of this:

CREATE FUNCTION MTD(date, decimal(11,2)

RETURNS DECIMAL(13,2)

RETURNS NULL ON NULL INPUT

.

The select would like like:

SELECT TRNDATE, AMOUNT, MTD(AMOUNT)

ORDER BY TRNDATE



And ideally the result would be a running total of month-to-
date
values.
Pretty slick.



What I found, though, leads me to believe that the MTD function
is
being
called before the ORDER BY, and that's giving me wildly
incorrect
results,
even when the source table is already in that sequence. When
the
source
table is already in the correct sequence and I drop the ORDER
BY, I
*seem*to* get the results that I want, but I doubt there's any
guarantee of
that; besides, that's a requirement that'll be hard to meet.
So my
two-part
question is: Is this expected behavior? Is there a way
around
this?



Dennis E. Lovelady
AIM/Skype: delovelady MSN: fastcounter@xxxxxxxxxxxx
<http://www.linkedin.com/in/dennislovelady>
www.linkedin.com/in/dennislovelady --
"A fanatic is one who can't change his mind and won't change
the
subject."
- Winston Churchill



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



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.