开发者

Making MEDIAN a function like AVG in SQL server

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 solu开发者_C百科tion to calculating medians here, 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)?


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜