Hi,
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
As an Amazon Associate we earn from qualifying purchases.