如何在postgresql中从我的查询中获得min,median和max

时间:2012-08-22 06:47:20

标签: postgresql

我写了一个查询,其中一列是一个月。从那里我必须得到最小月,最大月和月中位数。以下是我的询问。

select ext.employee,
       pl.fromdate,
       ext.FULL_INC as full_inc,
       prevExt.FULL_INC as prevInc,
       (extract(year from age (pl.fromdate))*12 +extract(month from age (pl.fromdate))) as month,
       case
         when prevExt.FULL_INC is not null then (ext.FULL_INC -coalesce(prevExt.FULL_INC,0))
         else 0
       end as difference,
       (case when prevExt.FULL_INC is not null then (ext.FULL_INC - prevExt.FULL_INC) / prevExt.FULL_INC*100 else 0 end) as percent
from pl_payroll pl
  inner join pl_extpayfile ext
          on pl.cid = ext.payrollid
         and ext.FULL_INC is not null
  left outer join pl_extpayfile prevExt
               on prevExt.employee = ext.employee
              and prevExt.cid = (select max (cid) from pl_extpayfile
                                 where employee = prevExt.employee
                                 and   payrollid = (
                                   select max(p.cid)
                                   from pl_extpayfile,
                                        pl_payroll p
                                   where p.cid = payrollid
                                   and   pl_extpayfile.employee = prevExt.employee
                                   and   p.fromdate < pl.fromdate
                                 )) 
              and coalesce(prevExt.FULL_INC, 0) > 0 
where ext.employee = 17 
and (exists (
    select employee
    from pl_extpayfile preext
    where preext.employee = ext.employee
    and   preext.FULL_INC <> ext.FULL_INC
    and   payrollid in (
      select cid
      from pl_payroll
      where cid = (
        select max(p.cid)
        from pl_extpayfile,
             pl_payroll p
        where p.cid = payrollid
        and   pl_extpayfile.employee = preext.employee
        and   p.fromdate < pl.fromdate
      )
    )
  )
  or not exists (
    select employee
    from pl_extpayfile fext,
         pl_payroll p
    where fext.employee = ext.employee
    and   p.cid = fext.payrollid
    and   p.fromdate < pl.fromdate
    and   fext.FULL_INC > 0
  )
)
order by employee,
         ext.payrollid desc

如果不可能获得最大月份和月份。

4 个答案:

答案 0 :(得分:70)

要计算 PostgreSQL 的中位数,只需占用50%的百分位数(无需添加额外的功能或任何内容):

SELECT PERCENTILE_CONT(0.5) WITHIN GROUP(ORDER BY x) FROM t;

答案 1 :(得分:15)

您需要名为minmax的聚合函数。请参阅PostgreSQL文档和教程:

PostgreSQL中没有内置中间值,但是已经实现并为wiki做出了贡献:

http://wiki.postgresql.org/wiki/Aggregate_Median

加载后,它与minmax的使用方式相同。用PL / PgSQL编写它会慢一点,但是如果速度至关重要的话,那里甚至还有一个C版本可以适应。

更新评论后:

听起来您想要显示统计汇总以及各个结果。您不能使用普通聚合函数执行此操作,因为您无法引用结果列表中不在GROUP BY中的列。

您需要从子查询中获取统计信息,或将聚合用作窗口函数。

给出虚拟数据:

CREATE TABLE dummystats ( depname text, empno integer, salary integer );
INSERT INTO dummystats(depname,empno,salary) VALUES
('develop',11,5200),
('develop',7,4200),
('personell',2,5555),
('mgmt',1,9999999);

...并在添加the median aggregate from the PG wiki之后:

您可以使用普通聚合执行此操作:

regress=# SELECT min(salary), max(salary), median(salary) FROM dummystats;
 min  |   max   |         median          
------+---------+----------------------
 4200 | 9999999 | 5377.5000000000000000
(1 row)

但不是这样:

regress=# SELECT depname, empno, min(salary), max(salary), median(salary)
regress-# FROM dummystats;
ERROR:  column "dummystats.depname" must appear in the GROUP BY clause or be used in an aggregate function

因为在聚合模型中没有意义显示平均值和单个值。您可以显示群组:

regress=# SELECT depname, min(salary), max(salary), median(salary) 
regress-# FROM dummystats GROUP BY depname;
  depname  |   min   |   max   |          median          
-----------+---------+---------+-----------------------
 personell |    5555 |    5555 | 5555.0000000000000000
 develop   |    4200 |    5200 | 4700.0000000000000000
 mgmt      | 9999999 | 9999999 |  9999999.000000000000
(3 rows)

...但听起来你想要个人价值观。为此,您必须使用window,这是PostgreSQL 8.4中的新功能。

regress=# SELECT depname, empno, 
                 min(salary) OVER (), 
                 max(salary) OVER (), 
                 median(salary) OVER () 
          FROM dummystats;

  depname  | empno | min  |   max   |        median         
-----------+-------+------+---------+-----------------------
 develop   |    11 | 4200 | 9999999 | 5377.5000000000000000
 develop   |     7 | 4200 | 9999999 | 5377.5000000000000000
 personell |     2 | 4200 | 9999999 | 5377.5000000000000000
 mgmt      |     1 | 4200 | 9999999 | 5377.5000000000000000
(4 rows)

另见:

答案 2 :(得分:0)

中位数的另一个选择:

create cards

答案 3 :(得分:0)

要找到中位数,请执行以下操作: 例如,假设我们表中有6000行。首先,我们需要从原始表中获取一半行(因为我们知道中值始终是中间值),因此这里的一半是6000(取3001为精确的2)中间值)。

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;