How do I exclude outliers from an aggregate query?

后端 未结 4 1815
情书的邮戳
情书的邮戳 2021-02-10 09:30

I\'m creating a report comparing total time and volume across units. Here a simplification of the query I\'m using at the moment:

SELECT  m.Unit,
        COUNT(         


        
4条回答
  •  旧时难觅i
    2021-02-10 09:54

    You can exclude the top and bottom x percentiles with NTILE

    SELECT m.Unit,
            COUNT(*) AS Count,
            SUM(m.TimeInMinutes) AS TotalTime
    FROM    
            (SELECT
                 m.Unit,
                 NTILE(20) OVER (ORDER BY m.TimeInMinutes) AS Buckets
             FROM
                 main_table m
             WHERE
                 m.unit <> '' AND m.TimeInMinutes > 0
            ) m
    WHERE   
          Buckets BETWEEN 2 AND 19
    GROUP BY m.Unit
    HAVING  COUNT(*) > 15
    

    Edit: this article has several techniques too

提交回复
热议问题