So say I have this table:
| Name | Role |
|---|---|
| First | Science |
| First | Math |
| First | Science |
| First | Math |
| Second | Science |
| Third | Math |
| Third | Math |
I want to display a column of duplicates for Name/Role ONLY if role is different in each group. So the final result should be like this:
| Name | Role |
|---|---|
| First | Science |
| First | Math |
This is the only person that has a different role for the same name (no matter how many times that specific combination is duplicated). That's why even though Third/Math is also duplicated, it doesn't matter because it's the same combination.
I tried doing a CTE as follows:
;with cte as (
Select Name, Role, ROW_NUMBER() over (partition by name order by name) as 'rownum1'
from U.Users
group by u.name, u.role)
so then select * from cte where rownum > 1 gets me my names of people that have this issue but it doesn't display the duplicate roles for that user. Not sure how I should approach it differently?
If I join the CTE table to the original Users table, I also get the single entries.