如何在 postgreSQL 中重复查询,并将结果添加到 table
How to repeat a query in postgreSQL, and add the results to a table
我需要 运行 查询 1000 次,并将结果添加到 table。我有以下代码,这是我想重复的代码:
Select max_gridco, count(max_gridco) as TaxLots, sum(population) as pop_sum
from
(
Select tlid, max_gridco, population
from (
select tlid, max_gridco, population, st_intersects(tle.geom, acres025.geom)
from tle, acres025
where max_gridco not in (0, 1)
order by RANDOM()
limit 1000
) as count
where st_intersects = 't'
order by max_gridco
) as gridcode_count
group by max_gridco;
有没有办法让我自动 运行 这 1000 次,并且在输出 table 中有一列包含 运行 数字?所以我的 table 看起来像下面这样:
运行个数 | max_gridco |税号 | pop_sum
我正在尝试在 PgAdmin3 中执行循环命令,但似乎无法获得正确的语法。
如果您需要插入 来自特定 select 的值,您可以使用:
INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;
(https://www.w3schools.com/sql/sql_insert_into_select.asp)
这就是你要找的吗?
使用DO
块或创建函数,例如:
DO
$$
begin
for i in 1..1000 loop
insert into save_to_table (Run number,max_gridco,TaxLots,pop_sum)
Select i, max_gridco, count(max_gridco) as TaxLots, sum(population) as pop_sum
from
(
Select tlid, max_gridco, population
from (
select tlid, max_gridco, population, st_intersects(tle.geom, acres025.geom)
from tle, acres025
where max_gridco not in (0, 1)
order by RANDOM()
limit 1000
) as count
where st_intersects = 't'
order by max_gridco
) as gridcode_count
group by max_gridco;
end loop;
end;
$$
;
我需要 运行 查询 1000 次,并将结果添加到 table。我有以下代码,这是我想重复的代码:
Select max_gridco, count(max_gridco) as TaxLots, sum(population) as pop_sum
from
(
Select tlid, max_gridco, population
from (
select tlid, max_gridco, population, st_intersects(tle.geom, acres025.geom)
from tle, acres025
where max_gridco not in (0, 1)
order by RANDOM()
limit 1000
) as count
where st_intersects = 't'
order by max_gridco
) as gridcode_count
group by max_gridco;
有没有办法让我自动 运行 这 1000 次,并且在输出 table 中有一列包含 运行 数字?所以我的 table 看起来像下面这样:
运行个数 | max_gridco |税号 | pop_sum
我正在尝试在 PgAdmin3 中执行循环命令,但似乎无法获得正确的语法。
如果您需要插入 来自特定 select 的值,您可以使用:
INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;
(https://www.w3schools.com/sql/sql_insert_into_select.asp)
这就是你要找的吗?
使用DO
块或创建函数,例如:
DO
$$
begin
for i in 1..1000 loop
insert into save_to_table (Run number,max_gridco,TaxLots,pop_sum)
Select i, max_gridco, count(max_gridco) as TaxLots, sum(population) as pop_sum
from
(
Select tlid, max_gridco, population
from (
select tlid, max_gridco, population, st_intersects(tle.geom, acres025.geom)
from tle, acres025
where max_gridco not in (0, 1)
order by RANDOM()
limit 1000
) as count
where st_intersects = 't'
order by max_gridco
) as gridcode_count
group by max_gridco;
end loop;
end;
$$
;