views:

140

answers:

2

Hello,

I am new to SQL server. I need to calculate the median of the time stamp values in my table,

Table1: _TimeStamp
2009-12-20 11:59:56.0
2009-12-20 11:59:56.5
2009-12-20 11:59:56.3
2009-12-20 11:59:56.4
2009-12-20 11:59:56.4
2009-12-20 11:59:56.9

There is a nice solution to calculating medians here, http://stackoverflow.com/questions/1342898/function-to-calculate-median-in-sql-server

For coding simplicity, I would love to implement this as a function in SQL, similar to SELECT AVG(_TimeStamp) FROM Table1

but implemented like SELECT MEDIAN(_TimeStamp) FROM Table1

Is it possible to save a series of SQL operations as a function that accepts an argument (_TimeStamp) and returns a value (the median)?

A: 

You could do this with a CLR custom aggregate. I'd be overkill, but technically possible :-).

onupdatecascade
+1  A: 

you can make a scalar valued function and pass a table valued function as a parameter to it. then return your operation result.

masoud ramezani
Is it possible to save a user defined function and then call it from any subsequent SQL query? Or does the user defined function have to be defined in any SQL query that uses it? Sorry to ask a basic question but I haven't found a good online guide to user defined functions.
KE
please see these links :http://www.sqlteam.com/article/user-defined-functionshttp://msdn.microsoft.com/en-us/library/aa175085%28SQL.80%29.aspx
masoud ramezani