Skip to content
Advertisement

SQL Assign Custom values to those rows with similar IDs

This is what my current table looks like. A new ‘status’ column is to be created for each ‘id’ that must have the below 3 values.

EXAMPLE: Id 2 will get 1, id 3 will be assigned 0 and id 4 will get 2. There can be only 2 similar ids in the id table (there are no 3 values of 2 or 4)

Advertisement

Answer

We can determine the status values by using aggregation:

Note that I assign a status value of -1 to any id which does not meet one of the three criteria. This would include any id which only appears once, among other edge cases.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement