简体   繁体   中英

Transform SQL SERVER table

i have this table table with evaluators and evaluatees.

Every row of the table above is a relationship between two people(First row:person a evaluates person b, Second row person a evaluates person c and so on).

I want to trasform this table into looking like this Wanted Output

So Each row will correspond one evaluator and each column will be people being evaluated by him. (Because some evaluators have less people to evaluate than others the remaining columns for someone who doesnt have that many evaluatees will be NULL.

Many thanks hope you can help me out

Since you have SQL Server tag so, i would use row_number() function with conditional aggregation :

select evalutor, 
       max(case when seq = 1 then evalutee end) as evalutee1,
       max(case when seq = 2 then evalutee end) as evalutee2, 
       max(case when seq = 3 then evalutee end) as evalutee3 
from (select t.*, row_number() over (partition by evalutor order by evalutee) as seq
      from table t
     ) t
group by evalutor; 

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