要使用“分组依据”一次更新多个不重复的行,请执行以下操作:
我遵循了 通过循环生成[0, 1001]中不重复的随机数的随机数 。
但我不能将其应用到我的案例中。当我这样做时:
update weighed_directed_edge set endpoint= trunc(1000 * random()+ 1)
from generate_series(1,10) group by 1 where startpoint= from_point;
更新端点ID,它抱怨:
ERROR: syntax error at or near "group"
LINE 1: ...nc(1000 * random()+ 1) from generate_series(1,10) group by 1.
我也尝试过:
insert into weighed_directed_edge (startpoint,endpoint)
values (from_point, trunc((1000 * random()+ 1) )
FROM generate_series(1, directed2number)
GROUP BY 1 ;
insert into weighed_directed_edge (startpoint, endpoint, costs)
select 1, trunc(1000 * random()+ 1) from generate_series(1,10) group by 1,1;
不工作。我需要从另一个表中的相同点集中选择起点和终点,以填充 weighed_directed_edge
表。
cross_point 表:有 1001 个交叉点。
表“public.cross_point” 专栏 |类型 |修饰符 --------------+---------+------------------------ -------------------------------------------------------- 交叉口|整数 | not null 默认 nextval ('intersection_intersection_seq'::regclass) x_轴|整数 |不为空 y 轴 |整数 |不为空 索引: “intersection_pkey”主键,btree(交集) “intersection_x_axis_key”唯一,btree(x_axis,y_axis)
加权定向边表:
表“public.weighed_directed_edge” 专栏 |类型 |修饰符 ----------+--------------------+------------ 起点|整数 | 终点|整数 | 成本|双精度 | 索引: “weighed_directed_edge_startpoint_key”唯一,btree(起点,终点) 外键约束: “weighed_directed_edge_endpoint_fkey”外键(端点)参考 cross_point(交点) “weighed_directed_edge_startpoint_fkey”外键(起点)参考 cross_point(交点)
一个随机数的端点(从1001点列中随机获取)对应于每个起点(从点列中顺序获取)。 端点的数量在[1,7]中。
- 费用将根据起点和终点之间的距离而定 端点。
要求:
起点和终点的组合应该是唯一的。
起点集包含表 cross_point 中的所有 1001 个交点
端点全部来自同一交点集。
每个星点最多有7个端点匹配,(7路路口最多),并且 端点匹配数量是随机选择的。
我在下面的代码中跳过了成本的计算:
--The largest number of intersection chosen is 7, but this could be altered
create or replace function popluate_weighed_directed_edge() returns void as $$
declare
from_point integer;
to_point integer;
directed2number integer; --the number of node this startpoint leads to
counter integer;
factor float;
weight numeric;
start_pointer record;
begin
for start_pointer in select * from cross_point
loop
from_point := start_pointer.intersection;
directed2number := trunc(Random()*7+1);
counter := directed2number;
while counter > 0
loop
insert into weighed_directed_edge (startpoint) select from_point from generate_series(1,10) ;
update weighed_directed_edge set endpoint= trunc(1000 * random()+ 1) from generate_series(1,10) group by 1 where startpoint= from_point ;
update weighed_directed_edge set costs= trunc(1000 * random()+ 1) from generate_series(1,10) group by 1 where startpoint= from_point ;
counter := counter - 1;
end loop;
end loop;
end
$$ language plpgsql;
I followed the help from Generate a random number of non duplicated random number in [0, 1001] through a loop .
But I can't apply that to my case. When I do:
update weighed_directed_edge set endpoint= trunc(1000 * random()+ 1)
from generate_series(1,10) group by 1 where startpoint= from_point;
to update endpointId it complains:
ERROR: syntax error at or near "group"
LINE 1: ...nc(1000 * random()+ 1) from generate_series(1,10) group by 1.
I also tried:
insert into weighed_directed_edge (startpoint,endpoint)
values (from_point, trunc((1000 * random()+ 1) )
FROM generate_series(1, directed2number)
GROUP BY 1 ;
insert into weighed_directed_edge (startpoint, endpoint, costs)
select 1, trunc(1000 * random()+ 1) from generate_series(1,10) group by 1,1;
Not working. I need to select startpoint and endpoint from the same points set from another table, to populate weighed_directed_edge
table.
cross_point table : there're 1001 intersections.
Table "public.cross_point" Column | Type | Modifiers --------------+---------+--------------------------------------------------------------------- intersection | integer | not null default nextval ('intersection_intersection_seq'::regclass) x_axis | integer | not null y_axis | integer | not null Indexes: "intersection_pkey" PRIMARY KEY, btree (intersection) "intersection_x_axis_key" UNIQUE, btree (x_axis, y_axis)
weighed_directed_edge table:
Table "public.weighed_directed_edge" Column | Type | Modifiers ------------+------------------+----------- startpoint | integer | endpoint | integer | costs | double precision | Indexes: "weighed_directed_edge_startpoint_key" UNIQUE, btree (startpoint, endpoint) Foreign-key constraints: "weighed_directed_edge_endpoint_fkey" FOREIGN KEY (endpoint) REFERENCES cross_point(intersection) "weighed_directed_edge_startpoint_fkey" FOREIGN KEY (startpoint) REFERENCES cross_point(intersection)
A random number of endpoint (get from the 1001 points column randomly) corresponds to every startpoint (get from the points column sequentially),.
The number of endpoint is in [1,7].- Costs will be based on the distance between the startpoint and
endpoint.
Requirements:
The combination of startpoint and endpoint should be unique.
Startpoints set contains all 1001 intersection from table cross_point
Endpoints comes all from the same intersection point set.
Every starpoint there're at most of 7 endpoint matches, (7 way road intersection is the maximum), and the
number of endpoint matching is selected randomly.
I skipped the calculation of costs in the following code:
--The largest number of intersection chosen is 7, but this could be altered
create or replace function popluate_weighed_directed_edge() returns void as $
declare
from_point integer;
to_point integer;
directed2number integer; --the number of node this startpoint leads to
counter integer;
factor float;
weight numeric;
start_pointer record;
begin
for start_pointer in select * from cross_point
loop
from_point := start_pointer.intersection;
directed2number := trunc(Random()*7+1);
counter := directed2number;
while counter > 0
loop
insert into weighed_directed_edge (startpoint) select from_point from generate_series(1,10) ;
update weighed_directed_edge set endpoint= trunc(1000 * random()+ 1) from generate_series(1,10) group by 1 where startpoint= from_point ;
update weighed_directed_edge set costs= trunc(1000 * random()+ 1) from generate_series(1,10) group by 1 where startpoint= from_point ;
counter := counter - 1;
end loop;
end loop;
end
$ language plpgsql;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在循环中,您可以绘制一个随机数,并且仅在它不存在时插入它(并减少循环计数器)。 伪代码:
Within the loop you could draw a random number and only insert it (and decrement the loopcounter) if it does not exist. Pseudocode: