Can the script for median be generalised and put into a function type structure
问题 We are using the following script to calculate the median: SELECT [Period] = 'amountPeriodA', [Median] = AVG(x.amountPeriodA) INTO #mediantable FROM ( SELECT r.customer, r.amountPeriodA, [RowASC] = ROW_NUMBER() OVER(ORDER BY r.amountPeriodA ASC, customer ASC), [RowDESC] = ROW_NUMBER() OVER(ORDER BY r.amountPeriodA DESC, customer DESC) FROM #MyExample r ) x WHERE RowASC IN (RowDESC, ROWDESC-1, ROWDESC+1) Is it possible to generalise a script like this and then code it into the server so that