I'm calculating median as:
DECLARE @TEMP TABLE ( ID INT ) Select ( ( Select Top 1 ID From ( Select Top 50 Percent ID From @Temp Where ID Is NOT NULL Order By ID ) As A Order By ID DESC ) + ( Select Top 1 ID From ( Select Top 50 Percent ID From @Temp Where ID Is NOT NULL Order By ID DESC ) As A Order By ID Asc ) ) / 2
Above query I want to use. But, in my case there are so many
columns for those I want to calculate
MEDIAN. But I think it would be bad to repeat above block of code for each
So, I'm trying to define separate function which would accept
column values, process and return median.
Should I have to define
table-value-funtion for that or there is another
optimized way to do so??
This question is related to the following questions:
- How to write function in sql which accept table as input and return result back as table?
- Getting error "must declare the scalar variable" in SQL function even though it is declared
- why unable to execute table-valued function?
- How to use UDF function in insert into select from clause?
- How to Insert/update table by calling function in it?