SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


userdefined function for average


userdefined function for average

Author
Message
afuchs 47083
afuchs 47083
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 14
Hi guys,

I tried to create a new userdefined function for calculating the average of top 10 values on different columns of my table:

Example:

SELECT AVG(a02) FROM (SELECT TOP 10 a02 FROM table01 WHERE pkart3='A' AND pkid='Y' ORDER BY lfnr DESC) u

This works fine. But I get an error on creating this function:

CREATE FUNCTION dbo.rollavg (@data_in char(3))
RETURNS int
AS
BEGIN
DECLARE @data_out int
SET @data_out = AVG(u.data) FROM (SELECT TOP 10 @data_in AS data FROM table01 WHERE pkart3='A' ORDER BY lfnr DESC) u
RETURN @data_out
END

I thought to use this function: SELECT dbo.rollavg(a02) AS [ra02] FROM table01

Can anyone help me?
Thanks a lot!
Abu Dina
Abu Dina
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2917 Visits: 3325
You're almost there!

Try this:

CREATE FUNCTION dbo.rollavg (@data_in char(3))
RETURNS int
AS
BEGIN
DECLARE @data_out int
SET @data_out = (SELECT [/color]AVG(u.data) FROM (SELECT TOP 10 @data_in AS data FROM table01 WHERE pkart3='A' ORDER BY lfnr DESC) u)

RETURN @data_out
END

---------------------------------------------------------


It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
David Edwards - Media lens

Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
Howard Zinn
afuchs 47083
afuchs 47083
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 14
Thanks for the answer. Unfortunately the function does not accept char-type for the AVG Operator. If I change @data_in to int (does not help because the column names are not int) the function is accepted.
So I decided to solve this little Problem with a view:
SELECT AVG(column1) rcol1, AVG(colmun2) rcol2, ... FROM (SELECT TOP 10 column1, column2, ... FROM table1 WHERE ...) u

Not nice but does the Job.
Anyhow thank for the help.
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40870 Visits: 20000
afuchs 47083 (4/4/2013)
Hi guys,

I tried to create a new userdefined function for calculating the average of top 10 values on different columns of my table:

Example:

SELECT AVG(a02) FROM (SELECT TOP 10 a02 FROM table01 WHERE pkart3='A' AND pkid='Y' ORDER BY lfnr DESC) u

This works fine. But I get an error on creating this function:

CREATE FUNCTION dbo.rollavg (@data_in char(3))
RETURNS int
AS
BEGIN
DECLARE @data_out int
SET @data_out = AVG(u.data) FROM (SELECT TOP 10 @data_in AS data FROM table01 WHERE pkart3='A' ORDER BY lfnr DESC) u
RETURN @data_out
END

I thought to use this function: SELECT dbo.rollavg(a02) AS [ra02] FROM table01

Can anyone help me?
Thanks a lot!


Yes - don't do it!!
For the sake of a little code simplification, the cost of using a scalar udf simply isn't worth it. Scalar udf's prohibit parallelism in all or part of the execution plan and are executed for every row of the part of the plan where they are called.
On the other hand, if you're seeking an average for a range of different values for pkart3 then there are tricks you could use, such as an iTVF or just a plain ol' CROSS APPLY.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search