I have a table like this:
account | check1 | check2
1 | 100]200]300 | 101]209]305
2 | 401]502 | 404]511
3 | 600 | 601
I want to separate the records into something like this:
account | check1 | check2
1 | 100 | 101
1 | 200 | 209
1 | 300 | 305
2 | 401 | 404
2 | 502 | 511
. | . | .
. | . | .
. | . | .
How do I do this using SQL server only?
Thanks,
First, you need a split function that can allow you to determine order within the result. This is a multi-statement TVF which uses an IDENTITY column
(If you have a Numbers table, you can use that instead of the subquery, and this will also allow you to add WITH SCHEMABINDING to the function’s definition, which provides potential performance benefits.)
With the function in place, here is sample usage given the data you’ve provided and desired results:
Results:
This assumes that you have some kind of validation / enforcement that corresponding values in check1 and check2 columns will always have the same number of values. It also assumes any check1 / check2 value will not exceed about 7,000 characters (again a Numbers table can help make that more flexible).
EDIT
After AndriyM’s comments I wanted to come back and re-visit this, mostly to supply a version of the above function which works without using a multi-statement TVF. This uses Andriy’s idea ROW_NUMBER() could be used.