Search code examples
sqlsql-serverdatabasemedianpercentile

Is there a way for me to round the float value to 4 decimals places generated by this percentile function? [MS-SQL SERVER]


SELECT DISTINCT percentile_disc(0.5) WITHIN GROUP(ORDER BY Number) OVER() FROM TABLE


Solution

  • If you want to get a single value, I'm not sure why you are expressing this as a table. I would expect code like this:

    DECLARE @median NUMERIC(20, 4);
    
    SELECT TOP (1) @median = round(percentile_disc(0.5) WITHIN GROUP (ORDER BY Number) OVER(), 4)
    FROM @table;
    
    SELECT @median;