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

Dee*_*mar 18 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
Run Code Online (Sandbox Code Playgroud)

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

Tob*_*ker 81

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

SELECT PERCENTILE_CONT(0.5) WITHIN GROUP(ORDER BY x) FROM t;
Run Code Online (Sandbox Code Playgroud)

  • 就像一个魅力,但观察到这是postgres 9.4+! (5认同)
  • 在许多情况下,`PERCENTILE_DISC()`可能是首选. (4认同)
  • 好的。我担心它不会平均长度集中的值,但它似乎工作得很好。`SELECT PERCENTILE_CONT(0.5) Within GROUP(ORDER by val) FROMgenerate_series(1, 4) as t(val);` 返回 2.5。然而,“PERCENTILE_DISC”返回 2。 (2认同)

Cra*_*ger 20

您想要名为min和的聚合函数max.请参阅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);
Run Code Online (Sandbox Code Playgroud)

...并在添加PG wiki的中间聚合之后:

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

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

但不是这个:

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
Run Code Online (Sandbox Code Playgroud)

因为在聚合模型中显示平均值与单个值一起没有意义.你可以显示组:

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)
Run Code Online (Sandbox Code Playgroud)

...但听起来你想要个人价值观.为此,您必须使用一个窗口,一个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)
Run Code Online (Sandbox Code Playgroud)

也可以看看: