sql - Can you run an UPDATE and WITH together? -


i trying run update on table, error

error:  syntax error @ or near "update" 

the query is:

with   first_users (     select min(created_at) first_at,     company_id company_id     users     group company_id   )  update companies  set first_seen_at = least(first_seen_at,     (select first_at first_users id = first_users.company_id)  ); 

can not run updates , withs together? seems weird.

my query more complex, why using syntax. when run select * first_users instead of update, works, there's wrong update keyword or something.

i suggest changing update . . . from in case. there no reason update records not match. so:

update companies    set first_seen_at = u.first_at    (select company_id, min(created_at) first_at          users          group company_id         ) u    companies.id = u.company_id ,          u.first_seen_at < companies.first_seen_at; 

postgres started supporting ctes updates in version 9.1 (http://www.postgresql.org/docs/9.1/static/sql-update.html vs http://www.postgresql.org/docs/9.0/static/sql-update.html). method better because filters rows before update.


Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -