I'm been looking for hours at different options (PIVOT, CROSS JOIN etc...) to transform an unknown number of rows to an unknown number of columns with SQL server 2008 and I'm even more lost than before I started.
Basically I have 3 tables:
Role
|id| name|
| 1|role1|
| 2|role2|
| 3|role3|
Action
|id| name |
| 1|action1|
| 2|action2|
| 3|action3|
RoleAction
|roleId| actionId|
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 3 | 2 |
Is there anyway to build a query or SP that would transform the result of a LEFT JOIN
to a nice "pivot" table like this:
|actionId|actionName|role1|role2|role3|.....|role n|
| 1 | action1 | 1 | 1 | 0 |.....| 0 |
| 2 | action2 | 1 | 0 | 1 |.....| 0 |
| 3 | action3 | 0 | 0 | 0 |.....| 0 |
| . | . | . | . | . |.....| . |
| . | . | . | . | . |.....| . |
| n | action n | 0 | 0 | 0 |.....| 0 |
Many many thanks for your help!