如何从以下查询中获得预期的结果,postgres 目前不支持在此处输入链接描述,(参见关于兼容性的最后一节)
更新帐户 SET (contact_last_name, contact_first_name) = (SELECT last_name, first_name FROM salesmen WHERE salesmen.id = accounts.sales_id);
实际上,我正在向表中添加一个新的空列,并用另一个表/视图中相同大小和类型的列的内容填充它
do $$DECLARE
k record;
_current_view text := '';
_current_var text := '';
_new_column_sql text := '';
_insert_sql text := '';
_select_sql text := '';
begin
for k in SELECT column_name from information_schema.columns where table_name='train'
and column_name <> 'action'
loop
_current_view := 'av_' || k.column_name;
_current_var := k.column_name;
_new_column_sql := 'alter table train_average_imputed add column ' || _current_var || ' float';
_select_sql := 'select prob as ' || _current_var || ' from ' || _current_view || ', train
where train.' || _current_var || '=' || _current_view || '.' || _current_var ;
_insert_sql := 'insert into train_average_imputed(' || _current_var || ') ' || _select_sql;
raise notice '%', _select_sql;
--EXECUTE _test_sql;
EXECUTE _new_column_sql;
EXECUTE _insert_sql;
exit;
end loop;
end$$;
谢谢。
更新
实际上我需要沿着行号更新,我尝试使用这个
drop table play2;
create table play2 as (select action from play);
alter table play2 add column color text;
with trans_table as(select color, row_number() over() as rn from play2)
update trans_table set color = a.color from (
select color, row_number() over() as rn from play) as a
where trans_table.rn =a.rn;
但我收到以下错误
DROP TABLE
SELECT 100
ALTER TABLE
ERROR: relation "trans_table" does not exist
LINE 2: update trans_table set color = a.color from (
^
我看不出我的代码与 postgreswith
页面在此处输入链接描述有何不同。请注意,如果我在它返回预期结果之后使用 aselect
而不是update
查询。with
更新 2
正如伊戈尔建议的那样,只需要复制表格并从那里添加列。