Wrox Home  
Search P2P Archive for: Go

  Return to Index  

sql_language thread: How can I make aggregate function?


Message #1 by "William Markham" <William.Markham@p...> on Tue, 15 Jan 2002 23:17:45
This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_001_01C19EDB.4FDA3914
Content-Type: text/plain

Messy. Whatever that is it is somewhat more complicated than my formula.
I'll just assume that it works.

SQL Server 2000 allows you to create user defined functions. I take it what
want to do is pass it a field something similar to COUNT()?

Unfortunately I don't have SQL2K, only SQL7 so all I have is the online
documentation so I cannot write and test something. The problem is that
functions take scalar values, not references to columns. What you could do
is pass the field name as a string, eg STEDEV('myField'). From that you
could generate a SELECT string to get the standard deviation and then
execute the string. You might run into problems getting the result of the
SQL string that is EXECUTEd back to the outer query.

Generating SQL strings and executing them is generally a bad idea. They are
a security hazard and are less efficient than other techniques. But if you
must have it as a function you could give this a shot.

regards
David Cameron
nOw.b2b
dcameron@i...

-----Original Message-----
From: William Markham [mailto:William.Markham@p...]
Sent: Thursday, 17 January 2002 1:54 AM
To: sql language
Subject: [sql_language] Re: How can I make aggregate function?


Here is the actual query I made for the equation:


Select Channel, Range_Bin, C = Sum(Cos(Radians(Angle1))), S = Sum(Sin
(Radians(Angle1))), 
	Sqrt(Square(Sum(Cos(Radians(Angle1)))) + Square(Sum(Sin(Radians
(Angle1))))) As R,
	(1 - ((Sqrt(Square(Sum(Cos(Radians(Angle1)))) 
		+ Square(Sum(Sin(Radians(Angle1))))))) / (Count(Angle1))) 
As Sam_CircVar, --V
	Degrees(Acos(Sum(Cos(Radians(Angle1))) 
		/ Sqrt(Square	(Sum(Cos(Radians(Angle1)))) 
		+ Square(Sum(Sin(Radians(Angle1))))))) As Mean_Direction,
	Sqrt(-2 * Log10 (1 - (1 - ((Sqrt(Square(Sum(Cos(Radians(Angle1)))) 
		+ Square(Sum(Sin(Radians(Angle1))))))) / (Count
(Angle1))))) As Circ_StdDev
From AngleBins
Group By Channel, Range_Bin
Order By Channel, Range_Bin


My goal is to be able to call it like you would a sqrt function.  
i.e.    Select SQRT(Some_Number)
        From Some_Table

Think it can be done in SQL?  Will this work if I made it into a sproc?
Thanks
Bill
$subst('Email.Unsub').


  Return to Index