开发者

Making MEDIAN a function like AVG in SQL server

开发者 https://www.devze.com 2022-12-20 05:07 出处:网络
I am new to SQL server.I need to calculate the median of the time stamp values in my table, Table1: _TimeStamp

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

精彩评论

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

关注公众号