My current data looks like this
OrderID | Category
1 | A
1 | A
1 | B
1 | B
1 | D
2 | B
3 | B
3 | B
3 | B
What I would like is a kind of pivot, which shows a count of how many times an OrderID is placed in a particular category. The exception is for category 'B'. Every time an OrderID belongs to category B, I would like to display it as an instance of B_x where x is the xth instance of B being called. Therefore OrderID 1 would have B_1 and B_2, OrderID 2 would have just B_1, OrderID 3 would have B_1, B_2 and B_3. Therefore, the final results would be similar to this
OrderID | A | B_1 | B_2 | B_3 | D
1 | 2 | 1 | 1 | 0 | 1
2 | 0 | 1 | 0 | 0 | 0
3 | 0 | 1 | 1 | 1 | 0
I'm using SQL for this, so any pointers would be greatly appreciated!