简体   繁体   English

转换 SQL SERVER 表

[英]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).上表的每一行都是两个人之间的关系(第一行:人a评估人b,第二行人a评估人c,依此类推)。

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. (因为有些评估者要评估的人比其他人少,对于没有那么多评估者的人来说,剩余的列将为 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 :既然你有 SQL Server 标签,我会使用row_number()函数和条件聚合:

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; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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