Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql中的游标和更新_Sql_Postgresql - Fatal编程技术网

Postgresql中的游标和更新

Postgresql中的游标和更新,sql,postgresql,Sql,Postgresql,我在postgresql中编写了一个函数,用于使用两个游标更新表,代码执行良好,没有错误,但不做任何事情 我有三张桌子 产品主控 销售数据表 目标表 因此,在下面的函数中,我从1中获取产品密钥,然后在销售表中计算产品销售的门店数,然后相应地更新第三个表 我需要每周计算每种产品的门店数量。但它们应该是独特的,例如,如果产品在第一周在a和B门店销售,第一周的门店数量应为2家,如果下周在B、C、D门店销售,第二周的总数应为4家,如果在第三周在a和D门店销售,第三周的总数仍应为4家。我该怎么做呢 任何帮

我在postgresql中编写了一个函数,用于使用两个游标更新表,代码执行良好,没有错误,但不做任何事情

我有三张桌子

产品主控 销售数据表 目标表 因此,在下面的函数中,我从1中获取产品密钥,然后在销售表中计算产品销售的门店数,然后相应地更新第三个表

我需要每周计算每种产品的门店数量。但它们应该是独特的,例如,如果产品在第一周在a和B门店销售,第一周的门店数量应为2家,如果下周在B、C、D门店销售,第二周的总数应为4家,如果在第三周在a和D门店销售,第三周的总数仍应为4家。我该怎么做呢

任何帮助都将不胜感激

  create or replace function StCount() returns void as
   $BODY$
    Declare
      ik1 integer ; 
    wk1  integer ; 
    Cur1 CURSOR FOR SELECT  ik  From table1 ; 
      Cur2   CURSOR FOR SELECT distinct(wk) FROM table2 order by wk asc;


  begin
    OPEN Cur1 ;
   WHILE (Found) Loop 
   FETCH  next from Cur1 INTO ik1  ; 

OPEN Cur2 ;

  WHILE (Found) Loop 
     FETCH  next from Cur2 INTO wk1;    

                update table3 set skly =(select count(sk)from table2 a  
        where a.ik = ik1   and a.wk = wk1  
        and a.sk not in (select distinct (sk) from table2
        where ik = ik1 and wk <= wk1 - 1 ))
        where ik = ik1 and wk = w1
        ;


  End loop ;
CLOSE Cur2;

  end loop ;

   CLOSE Cur1;

  END;
   $BODY$
   LANGUAGE plpgsql VOLATILE;

您没有向我们展示您的表是什么样子,或者您正在处理的数据到底是什么,所以我在这里假设了很多

使用此设置:

create table product (id integer primary key);
create table sales (id serial primary key, product_id integer, store text, wk integer);

insert into product values (1),(2),(3),(4);

insert into sales (product_id, store, wk)
values
  (1, 'A', 1),
  (1, 'B', 1),
  (1, 'B', 2),
  (1, 'C', 2),
  (1, 'D', 2),
  (1, 'A', 3),
  (1, 'D', 3);
每个产品、商店和每周的累计销售额可以这样计算:

select product_id, 
       store,
       wk,
       count(store) over (partition by product_id order by wk) as sofar
from (
  select product_id, 
         store, 
         wk,
         row_number() over (partition by product_id, store order by wk) as rn
  from sales
  order by product_id, store
) t
where rn = 1;
请注意,product表实际上并未用于此查询,但它可以轻松地连接到内部查询

对于上述示例数据,这将返回:

product_id | store | wk | sofar
-----------+-------+----+------
         1 | A     |  1 |     2
         1 | B     |  1 |     2
         1 | C     |  2 |     4
         1 | D     |  2 |     4
现在可以使用此结果更新目标表:

update target_table
   set slky = t.sofar
from (
    select product_id, 
           store,
           wk,
           count(store) over (partition by product_id order by wk) as sofar
    from (
      select product_id, 
             store, 
             wk,
             row_number() over (partition by product_id, store order by wk) as rn
      from sales
      order by product_id, store
    ) t
    where rn = 1
) s
where s.wk = target_table.wk 
  and s.product_id = target_table.product_id
  and s.store = target_table.store;

这假设product_id、store、wk的组合在target_表中是唯一的。由于在示例中混淆了表和列的名称,因此很难分辨

您没有向我们展示您的表是什么样子,或者您正在处理的数据是什么,所以我在这里假设了很多

使用此设置:

create table product (id integer primary key);
create table sales (id serial primary key, product_id integer, store text, wk integer);

insert into product values (1),(2),(3),(4);

insert into sales (product_id, store, wk)
values
  (1, 'A', 1),
  (1, 'B', 1),
  (1, 'B', 2),
  (1, 'C', 2),
  (1, 'D', 2),
  (1, 'A', 3),
  (1, 'D', 3);
每个产品、商店和每周的累计销售额可以这样计算:

select product_id, 
       store,
       wk,
       count(store) over (partition by product_id order by wk) as sofar
from (
  select product_id, 
         store, 
         wk,
         row_number() over (partition by product_id, store order by wk) as rn
  from sales
  order by product_id, store
) t
where rn = 1;
请注意,product表实际上并未用于此查询,但它可以轻松地连接到内部查询

对于上述示例数据,这将返回:

product_id | store | wk | sofar
-----------+-------+----+------
         1 | A     |  1 |     2
         1 | B     |  1 |     2
         1 | C     |  2 |     4
         1 | D     |  2 |     4
现在可以使用此结果更新目标表:

update target_table
   set slky = t.sofar
from (
    select product_id, 
           store,
           wk,
           count(store) over (partition by product_id order by wk) as sofar
    from (
      select product_id, 
             store, 
             wk,
             row_number() over (partition by product_id, store order by wk) as rn
      from sales
      order by product_id, store
    ) t
    where rn = 1
) s
where s.wk = target_table.wk 
  and s.product_id = target_table.product_id
  and s.store = target_table.store;

这假设product_id、store、wk的组合在target_表中是唯一的。由于在示例中混淆了表和列的名称,因此很难分辨

首先,为什么要使用游标,使用单个update语句可以更有效地执行此操作,distinct不是一个函数我只是选择distinct值来比较新的数字,你在哪里看到我将distinct定义为一个函数?我使用光标,因为每次我都要扫描整个表来计算我需要的值,我不确定一次更新如何实现DISTINCTWK-distinct不是一个函数。这和独特的作品是一样的。distinct始终对“选择”列表中的所有列进行操作。如果选择列表中的列是单列且多列完全错误,则将其放在括号中是无用的。另外:子查询中的distinct也无用。请提问并添加相关表的定义、一些示例数据以及更新后的预期输出。我相信这可以在没有高效和慢速游标循环的情况下完成。为什么您首先要使用游标,使用单个update语句可以更高效地完成此操作,而distinct不是一个函数我只是选择distinct值来比较新数字,你在哪里看到我将distinct定义为一个函数?我使用光标,因为每次我都要扫描整个表来计算我需要的值,我不确定一次更新如何实现DISTINCTWK-distinct不是一个函数。这和独特的作品是一样的。distinct始终对“选择”列表中的所有列进行操作。如果选择列表中的列是单列且多列完全错误,则将其放在括号中是无用的。另外:子查询中的distinct也无用。请提问并添加相关表的定义、一些示例数据以及更新后的预期输出。我相信这可以在没有高效和慢速光标循环的情况下完成。非常感谢,它确实起了作用。但我仍然不确定我的光标哪里出了问题…非常感谢,它确实起了作用。但我仍然不确定我的光标哪里出了问题。。。