mercredi 22 septembre 2021

New column based on conditions of multiple columns

I would like to add a new column based on the occurrence of an event for each individual (person_id) and which occurrence this is for that person by using the person_id column and date column which is in YYYY-MM-DD format. Some dates may be null.

For example, if it's the first time person '1234' is present in the table, based on the date, then return 'start', if it's the second, 'ongoing', if it's the third, 'complete' and if there's no date, e.g. null, then 'incomplete'. The columns below already exist, except the 'status' column, which would be the name of this newly created column.

I've tried various case when then scripts as well as partitions by can't quite seem to crack this for every outcome in one hit.

Desired result:

person_id activity combination_count date status
1234 activity_1 1 2016-04-01 start
1234 activity_1 2 2016-05-04 complete
1234 activity_2 1 null incomplete
5678 activity_1 1 2019-09-01 start

Aucun commentaire:

Enregistrer un commentaire