Home > database >  Create a column based on values of 3 other columns
Create a column based on values of 3 other columns

Time:11-27

I have group and user data that looks like this:

enter image description here

I need to create a column that indicates whether or not the members of one group are from different companies.

So for Group A, I'd need the new column to indicate the group is mixed because it has a member from Company 1 and a member from Company 2.

Group B, is non-mixed group because all members are from Company 1.

Is this possible?

(SSMS 2016)

CodePudding user response:

You can do something like the following using Window functions

select *, 
    Iif(Min(membercompany) over(partition by groupname)
      =Max(membercompany) over(partition by groupname),'Same','Mixed')
from t
  • Related