cbunn
cbunn

Reputation: 205

SQL Insert newest value from another table

I have two PostgreSQL tables and I want to insert the most recent value from a column in one table into the other. I've tried something like this but I can't get it to work:

 INSERT INTO inland (lt_dispatch_level)
 SELECT named_lt_dispatch_level
 FROM obs
 WHERE created_at = (
 SELECT MAX(created_at) 
 FROM obs)

I need my inland.lt_dispatch_level = the most recent value from obs.named_lt_dispatch_level. Is there a way to select only that value?

Upvotes: 1

Views: 131

Answers (1)

NullEverything
NullEverything

Reputation: 460

Since you made it clear that you are trying to update a column in another, you need to utilize an update statement, not an insert. I went ahead and edited the answer.

Try This:

UPDATE inland SET lt_dispatch_level = obs.named_lt_dispatch_level 
FROM obs WHERE obs.created_at = (SELECT MAX(created_at) 
FROM obs) AND inland.cartodb_id = 1

The query should return you with the latest row.

Upvotes: 2

Related Questions