获取总值的前10%、20%和30%值的postgresql过程

获取总值的前10%、20%和30%值的postgresql过程,postgresql,stored-procedures,Postgresql,Stored Procedures,我有一个名为Scoreboard的表,其中包含一个名为score的字段,它是一个数组,包含值27,56,78,12,89,77,34,23,90,87,33,55,30,67,76,87,56,我想编写一个PostgreSQL过程来获取三个类别 类别1=数组中值总数的前10% 类别2=数组中值总数的前20% 类别3=数组中值总数的前30% 并以相同的格式将其放入数组中,即 [1类值、2类值、3类值]smth这样做应该: t=# with p as ( with ntile as (

我有一个名为Scoreboard的表,其中包含一个名为score的字段,它是一个数组,包含值27,56,78,12,89,77,34,23,90,87,33,55,30,67,76,87,56,我想编写一个PostgreSQL过程来获取三个类别 类别1=数组中值总数的前10% 类别2=数组中值总数的前20% 类别3=数组中值总数的前30% 并以相同的格式将其放入数组中,即
[1类值、2类值、3类值]

smth这样做应该:

t=# with p as (
  with ntile as (
    with v as (
      select unnest('{27,56,78,12,89,77,34,23,90,87,33,55,30,67,76,87,56}'::int[]) a
    )
    select a,ntile(10) over(order by a desc)
    from v
  )
  select distinct  string_agg(a::text,',') over (partition by ntile),ntile
  from ntile
  where ntile <=3 order by ntile
)
select array_agg(concat('category ',ntile,' ',string_agg))
from p;
                         array_agg
------------------------------------------------------------
 {"category 1 90,89","category 2 87,87","category 3 78,77"}
(1 row)

Time: 0.493 ms

像这样的smth应该做到:

t=# with p as (
  with ntile as (
    with v as (
      select unnest('{27,56,78,12,89,77,34,23,90,87,33,55,30,67,76,87,56}'::int[]) a
    )
    select a,ntile(10) over(order by a desc)
    from v
  )
  select distinct  string_agg(a::text,',') over (partition by ntile),ntile
  from ntile
  where ntile <=3 order by ntile
)
select array_agg(concat('category ',ntile,' ',string_agg))
from p;
                         array_agg
------------------------------------------------------------
 {"category 1 90,89","category 2 87,87","category 3 78,77"}
(1 row)

Time: 0.493 ms

我假设您有一个表,其中一列作为id,另一列是数组类型。基于假设 我创建了如下表,并在其中插入了两个值

create table test_array (id int, values int[]);
insert into test_array values(1 ,'{27,56,78,12,89,77,34,23,90,87,33,55,30,67,76,87,56}' );
insert into test_array values(2 ,'{72,65,84,21,98,77,43,32,9,78,41,66,3,76,67,88,56}' );
下面是用于查找您提到的类别的函数。如果表中没有任何id列 然后,您可以使用提示添加数字:row\u number

create or replace function find_category() returns table(category text[]) as 
$$
BEGIN

return query with unnestColumn as (
select id, unnest(values) as values, ntile(10) over(partition by id order by unnest(values) desc) as ntilenumber
from test_array 
) ,groupedCategory as ( select id, ntilenumber, string_agg(values::text,',') as combinedvalues from unnestColumn 
where 
ntilenumber <= 3 
group by id, ntilenumber ) 
select array_agg(concat('Categoty',ntilenumber, ' ', combinedvalues ))
from groupedCategory
group by id;

END;
$$
language 'plpgsql';

我假设您有一个表,其中一列作为id,另一列是数组类型。基于假设 我创建了如下表,并在其中插入了两个值

create table test_array (id int, values int[]);
insert into test_array values(1 ,'{27,56,78,12,89,77,34,23,90,87,33,55,30,67,76,87,56}' );
insert into test_array values(2 ,'{72,65,84,21,98,77,43,32,9,78,41,66,3,76,67,88,56}' );
下面是用于查找您提到的类别的函数。如果表中没有任何id列 然后,您可以使用提示添加数字:row\u number

create or replace function find_category() returns table(category text[]) as 
$$
BEGIN

return query with unnestColumn as (
select id, unnest(values) as values, ntile(10) over(partition by id order by unnest(values) desc) as ntilenumber
from test_array 
) ,groupedCategory as ( select id, ntilenumber, string_agg(values::text,',') as combinedvalues from unnestColumn 
where 
ntilenumber <= 3 
group by id, ntilenumber ) 
select array_agg(concat('Categoty',ntilenumber, ' ', combinedvalues ))
from groupedCategory
group by id;

END;
$$
language 'plpgsql';

谢谢,但实际上我想把它放在一个过程中,而且我忘了提到数组在表中,所以我们必须从表中获取值。你可以将上面的函数包装成sql函数。另外,请尝试在第一次尝试时提问-否则我们不会回答问题,而是在这里聊天支持谢谢,但实际上我希望它在一个过程中,而且我忘了提到数组在表中,所以我们必须从该表中获取值。您可以将上面的函数包装为sql函数。另外,请尝试从第一次尝试开始提问-否则我们不会回答问题,而是在这里聊天支持