Skip to content
Advertisement

Query to generate map table to matrix table

I have 1 map table

Group Task
----- -----
Admin Add
Admin Edit
Admin Delete
Admin View
User  View

I need to generate it in a matrix table like

Task    Admin   User
------  ------  ------
Add     Yes     No
Edit    Yes     No
Delete  Yes     No
View    Yes     Yes

It will help if someone could suggest how SQL Server can generate this output?

Advertisement

Answer

Use conditional aggregation:

select
    task,
    max(case when [group] = 'Admin' then 'Yes' else 'No' end) Admin,
    max(case when [group] = 'User'  then 'Yes' else 'No' end) User
from mytable
group by task

This works because 'Yes' > 'No'.

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