so I got Questionnaire Data where the same Questions got asked multiple times, sadly they didnt think of giving them different question_ids, but at least i have the timestamps of when they were answered. Similar to this
person_id | question_id | answer | timestamp |
---|---|---|---|
12 | q1l1 | 0.4 | 12:38 |
12 | q1l1 | 0.7 | 12:54 |
Is there a easy way to change the question id based on timestamps?
So to grab dublicates of the same question_id for the same person_id and compare the timestamps and set the question_ids to q1l1_1 and q1l1_2
person_id | question_id | answer | timestamp |
---|---|---|---|
12 | q1l1_1 | 0.4 | 12:38 |
12 | q1l1_2 | 0.7 | 12:54 |
2
Answers
Window function is your approach:
sql online editor
You can use
ROW_NUMBER()
function as the following:If you want only select the new question_id:
If you want to update question_id to the new value:
See a demo.
I think you don’t need the condition
table_name.question_id=D.question_id
since all question_ids are the same for the same user.