Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to overwrite a row_number value in SQL?

How to overwrite a column in SQL (Redshift) ?

The following query gives me an error Target table must be part of an equijoin predicate

UPDATE table1
SET rank = temp.new_rank
FROM (
      SELECT ROW_NUMBER() OVER (PARTITION BY session_id ORDER BY date) AS new_rank
      FROM table1
      ) temp;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need a WHERE clause that matches each row of the table with a row of the subquery:

UPDATE table1 AS t1
SET rank = t.new_rank
FROM (
  SELECT *, ROW_NUMBER() OVER (PARTITION BY session_id ORDER BY date) AS new_rank
  FROM table1
) AS t
WHERE t.session_id = t1.session_id AND t.date = t1.date;

I use the columns session_id and date in the WHERE clause, but if there is another column, like a unique id or any other primary key it would be better to use that.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading