29

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

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

4 回答 4

132

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

SELECT PERCENTILE_CONT(0.5) WITHIN GROUP(ORDER BY x) FROM t;
于 2016-10-29T07:45:15.123 回答
25

您想要名为minand的聚合函数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);

...并从 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)

也可以看看:

于 2012-08-22T07:00:27.407 回答
0

中位数的另一种选择:

SELECT x
FROM table
ORDER BY x
LIMIT 1 offset (select count(*) from x)/2
于 2019-12-01T21:07:18.413 回答
0

要找到中位数:例如考虑我们在表中有 6000 行。首先我们需要从原始表中取出一半行(因为我们知道中位数总是中间值)所以这里 6000 的一半是 3000(取 3001以获得精确的两个中间值)。

SELECT *
      FROM (SELECT column_name
            FROM Table_name
            ORDER BY column_name
            LIMIT 3001)As Table1
      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;
于 2020-07-31T15:36:24.063 回答