Median comment length (sampled)

0

Please login or register to vote for this query.

(click on this box to dismiss)

Stack Overflow

Q&A for professional and enthusiast programmers

-- Code based on http://stackoverflow.com/questions/848872/select-n-random-rows-from-sql-server-table/848908#848908
with comment_length as (select top 10000 len(text) length 
                        from comments 
                        order by newid())

-- Code discovered via http://stackoverflow.com/questions/7227679/median-values-in-t-sql/7229009#7229009
SELECT ((
        SELECT TOP 1 Total
        FROM   (
                SELECT  TOP 50 PERCENT length Total
                FROM    comment_length A
                WHERE   length IS NOT NULL
                ORDER BY length
                ) AS A
        ORDER BY Total DESC) +
        (
        SELECT TOP 1 Total
        FROM   (
                SELECT  TOP 50 PERCENT length Total
                FROM    comment_length A
                WHERE   length IS NOT NULL
                ORDER BY length DESC
                ) AS A
        ORDER BY Total ASC)) / 2

Enter Parameters

Options:
Switch sites:
loading Hold tight while we fetch your results