How do i get min, median and max from my query in postgresql

后端 未结 4 1135
逝去的感伤
逝去的感伤 2020-12-08 14:50

I have written a query in which one column is a month. From that I have to get min month, max month, and median month. Below is my query.

select ext.employee         


        
4条回答
  •  南方客
    南方客 (楼主)
    2020-12-08 15:49

    To find Median: for instance consider that we have 6000 rows present in the table.First we need to take half rows from the original Table (because we know that median is always the middle value) so here half of 6000 is 3000(Take 3001 for getting exact two middle value).

    SELECT *
          FROM (SELECT column_name
                FROM Table_name
                ORDER BY column_name
                LIMIT 3001)
          ORDER BY column_name DESC ---->Look here we used DESC(Z-A)it will display the last 
                                    --   two values(using LIMIT 2) i.e (3000th row and 3001th row) from 6000 
                                    --   rows  
          LIMIT 2;
    

提交回复
热议问题