I have a database staging table that is used to store data fed in from another system before I use it to populate several database tables.
I was relying on the “primary keys” in the data getting fed in being unique, however it turns out that they are not unique at all e.g. the key “1” can apply to “Value A” and “VALUE B” thus it is obviously not a primary key!!!
As I no longer trust the data I was thinking of generating my own keys based on the contents of the staging table. E.g. for “Column A” I want to generate a unique integer based key based on the distinct values in that column. Is this possible via an UPDATE SET FROM or another mechanism?
You can use
dense_rank()and a CTE to number the rows.Try here: https://data.stackexchange.com/stackoverflow/q/122782/