0

我需要一些有关以下 postgres 功能的帮助

我有下表的列:

array, array_length

我最初有几个数组,然后我运行一个查询(实际上是一组 3 个查询)来选择数组,附加它们,然后将附加的数组插入到表中。

我需要循环这个插入查询,直到其中一个数组 ( select max(array_length) from table) 达到预定义的大小,比如长度 50。

我基本上需要写类似的东西

begin 
for (select max(array_length) from table)<50
loop 
(drop table if exists super_item_temp;
ALTER TABLE super_item
    RENAME TO super_item_temp;

create table super_item as 
select distinct * from super_item_temp;

insert into super_item 
select ...calculations... from super_item)
end

但我找不到正确的语法来写这个

我查看了http://www.postgresql.org/docs/8.4/static/plpgsql-control-structures.html上的手册, 但找不到任何有用的东西。

任何指向我正确方向的提示或链接将不胜感激!谢谢你。

编辑:我试过

创建或替换函数 function_name() RETURNS int4 AS ' DECLARE r RECORD;

BEGIN WHILE (从 super_item 中选择 max(array_length))<50

loop
drop table if exists super_item_temp;
    ALTER TABLE super_item
        RENAME TO super_item_temp;

    create table super_item as 
    select distinct * from super_item_temp;


    insert into super_item 
    select old_array, 
    array_sort_unique( array_agg(added_item) || a.old_array) as new_array, 
    array_length(array_sort_unique( array_agg(added_item) || a.old_array),1)

    from (
            select 
    a.new_array as old_array,

    case when string_to_array(b.item2::text, ",")::int[] <@ a.new_array and string_to_array(b.item1::text, ",")::int[] <@ a.new_array then null
     when string_to_array(b.item2::text, ",")::int[] <@ a.new_array then b.item1 else b.item2 end  as added_item

    from

    super_item a 
    left join pairs b 

    on  string_to_array(b.item1::text, ",")::int[] <@ a.new_array or  string_to_array(b.item2::text, ",")::int[] <@ a.new_array /**any item from pair is in array**/
    where 1=1

    group by a.new_array, 2

    having sum(b.count)>10
    and sum(b.offset)<=0 
    and 
    case 
    when string_to_array(b.item2::text, ",")::int[] <@ a.new_array and string_to_array(b.item1::text, ",")::int[] <@ a.new_array then null
    when string_to_array(b.item2::text, ",")::int[] <@ a.new_array then b.item1 else b.item2 end is not null
    /**new item is not null**/
    order by 2 desc
    )a
    group by 1;
END LOOP;

返回 1;结尾; ' 语言 plpgsql;

选择 function_name() 作为输出;

现在我收到一个函数参数的错误“未知列”,它应该是一个带引号的分隔符。

错误:列“,”存在于第 18 行:在 string_to_array(b.item1::text, ",")::int[] <@ a.new_ar...

4

1 回答 1

1

试试这个,在我的电脑上它编译得很好(但我不知道它是否按预期工作)。

CREATE OR REPLACE FUNCTION append_super_item ()
RETURNS integer AS $length$
declare
    length integer;
BEGIN
  WHILE (select max(array_length) from super_item)<50
    LOOP
       drop table if exists super_item_temp;
       ALTER TABLE super_item
            RENAME TO super_item_temp;
       Create table super_item as 
         select distinct * from super_item_temp;
       insert into super_item 
    select old_array, 
    array_sort_unique( array_agg(added_item) || a.old_array) as new_array, 
    array_length(array_sort_unique( array_agg(added_item) || a.old_array),1)

    from (
            select 
    a.new_array as old_array,

    case when string_to_array(b.item2::text, ",")::int[] <@ a.new_array and string_to_array(b.item1::text, ",")::int[] <@ a.new_array then null
     when string_to_array(b.item2::text, ",")::int[] <@ a.new_array then b.item1 else b.item2 end  as added_item

    from

    super_item a 
    left join pairs b 

    on  string_to_array(b.item1::text, ",")::int[] <@ a.new_array or  string_to_array(b.item2::text, ",")::int[] <@ a.new_array /**any item from pair is in array**/
    where 1=1

    group by a.new_array, 2

    having sum(b.count)>10
    and sum(b.offset)<=0 
    and 
    case 
    when string_to_array(b.item2::text, ",")::int[] <@ a.new_array and string_to_array(b.item1::text, ",")::int[] <@ a.new_array then null
    when string_to_array(b.item2::text, ",")::int[] <@ a.new_array then b.item1 else b.item2 end is not null
    /**new item is not null**/
    order by 2 desc
    )a
    group by 1;
    end loop;
end $length$ LANGUAGE plpgsql;
于 2013-10-24T09:58:59.000 回答