Issue Overview
I have a set of numbers that repeat, and I need to allocate group numbers to each set. For instance, when the numbers 1 to 5 show up repeatedly, the first group should be labeled as 1, the second as 2, and so on.
Example Input:
1
2
3
4
5
1
2
3
4
5
Expected Output:
Value | GroupNum
1 1
2 1
3 1
4 1
5 1
1 2
2 2
3 2
4 2
5 2
Application Scenario
In my case, I want to reformat normalized data into an easier-to-read layout. My data is structured like this:
ID | TypeCol | Info
1 1 01/15/2023
1 2 John
1 3 USA
1 4 1
1 1 02/15/2023
1 2 Mary
1 3 Canada
1 4 0
The TypeCol values mean:
- 1 = Date
- 2 = Name
- 3 = Country
- 4 = Availability (1, 0, or null)
Desired Format:
ID | Date | Name | Country | Availability
1 01/15/2023 John USA 1
1 02/15/2023 Mary Canada 0
I attempted to utilize ROW_NUMBER() but wasn’t able to implement the grouping correctly. How can I perform this conversion in SQL?