1

I need to update a Table using information from other table. This is my sql query:

UPDATE service a 
JOIN agency b USING (feed_id) 
SET end_date = (SELECT MIN(start_date)-1 
                FROM service c 
                JOIN agency d USING (feed_id) 
                WHERE b.feed_id = a.feed_+1 
                      AND b.agency_id = d.agency_id)

I need agency id in the condition so the only way is join both table with agency table.

Postgres return:

ERROR:  syntax error at or near "JOIN"
LINE 1: UPDATE service a JOIN agency b USING (feed_id) SET end_date ...

Any tips?

2
  • "doesn't work" is not a valid Postgres error message. Where in the manual did you find that syntax? Commented Oct 27, 2016 at 14:24
  • Please edit your question add the create table statements for the tables in question, some sample data and the expected result based on that data. Formatted text please, no screen shots Commented Oct 27, 2016 at 14:41

2 Answers 2

1

Try this :)

UPDATE service
SET end_date = (SELECT MIN(start_date)-1 
                FROM service c 
                JOIN agency d USING (feed_id) 
                WHERE b.feed_id = a.feed_+1 
                      AND b.agency_id = d.agency_id)
FROM service a
JOIN agency b USING (feed_id) 
Sign up to request clarification or add additional context in comments.

3 Comments

Do not repeat the target table in the FROMclause of an UPDATE statement.
i take syntax error at or near "JOIN" if i doesn't
Doesn't work. It's better add second join in the subquery
0

This query works:

UPDATE service a
SET end_date = (SELECT MIN(start_date)-1
            FROM service b
            JOIN agency c ON b.feed_id = c.feed_id
            JOIN agency d ON a.feed_id = d.feed_id 
            WHERE b.feed_id = a.feed_id+1 
            AND c.agency_id = d.agency_id
            )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.