简体   繁体   中英

Create a column based on values of 3 other columns

I have group and user data that looks like this:

在此处输入图像描述

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM