Skip to content
Advertisement

SQL: how to add a column that works as a composite key with another column

I have an SQL table that looks like this:

And I want to add a column that works as an inner ID for each Category such that Category and the new column could work as a composite key. The new column should look like this:

How can I write a query to achieve that?

Advertisement

Answer

You need to give a sequential order in partitions based Category and ordered by ID. This can be achieved using the row_number() window function.

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