Skip to content
Advertisement

How to insert new rows based on values in other columns in sql server?

I have a dataset like below:

What I need to do is to get the output below:

As to be seen, my purpose is to add a Code column and duplicate rows by labelling the code. The query below gives me how many times I need to add row with different code for each row, but couldn’t figure out an efficient way to do the rest.

Any help would be appreciated.

Advertisement

Answer

I would use apply:

The subquery is actually unnecessary. You might find this simpler:

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