PostgreSQL找到第n个数字

时间:2018-12-07 08:20:28

标签: postgresql

写一个在列中找到第三大整数的聚合? 这就是我在做什么:

create function third_step(bigint, integer) returns bigint AS
$$

SELECT distinct
 * from count_lab order by ab asc limit 1 offset 2 ;


$$ language sql; --can we use plpgsql somehow?

create aggregate third_agg(integer)
(stype=bigint,sfunc=third_step,initcond=0); 
SELECT distinct
 * from count_lab order by ab asc limit 1 offset 2  ;

2 个答案:

答案 0 :(得分:0)

创建函数third_step(整数,ab整数)返回整数AS $$

选择与众不同  *从count_lab顺序开始,按ab asc限制1偏移2;

$$语言sql; -我们可以以某种方式使用plpgsql吗?

创建聚合的third_agg(integer) (stype = integer,sfunc = third_step,initcond = 0); 选择不同  *从count_lab顺序开始,按ab asc限制1偏移2;

答案 1 :(得分:0)

应该是:

SELECT DISTINCT ab 
    FROM count_lab 
    WHERE ab IS NOT NULL 
    ORDER BY ab DESC 
    LIMIT 1 OFFSET 2;