如何在SQL Server中将员工值行分隔为列

如何在SQL Server中将员工值行分隔为列,sql,sql-server,database,Sql,Sql Server,Database,我正在寻找一种在SQL Server中将员工记录行转换为自动列的有效方法,我听说PIVOT速度不是很快,我需要处理大量记录 我有下面这样的数据记录 EmployeeNumber Employee Name Account Number -------------------------------------------------- 100204 Brody, David 4125641146 100204 Brody, Davi

我正在寻找一种在SQL Server中将员工记录行转换为自动列的有效方法,我听说PIVOT速度不是很快,我需要处理大量记录

我有下面这样的数据记录

EmployeeNumber  Employee Name       Account Number
--------------------------------------------------
100204          Brody, David        4125641146
100204          Brody, David        5300880081

100239          Moland, Luke        34901876
100239          Moland, Luke        0441360299
这是我的结果:员工有多个账号,我希望看起来像我的数据

  EmployeeNumber    Employee Name   AccountNumber  AccountNumber1
 -----------------------------------------------------------------
   100204           Brody, David      4125641146    5300880081
   100239           Moland, Luke      34901876      0441360299
我如何建立结果?谁能帮我得到结果吗?

你可以用下面的方法试试

select EmployeeNumber,EmployeeName,
max(case when rn=1 then AccountNumber end) as  AccountNumber  ,
max(case when rn=2 then AccountNumber end) as  AccountNumber1
from
(
select EmployeeNumber,  EmployeeName,   AccountNumber,row_number() over(partition by EmployeeName order by AccountNumber) as rn
from tablename
)A group by EmployeeNumber,EmployeeName

员工可以拥有多少个帐号?是否有最大?maxium 4帐号?您可以从@fa06编辑以下答案,以查看
rn=3
rn=4
是否允许
null
条件。我在“parion”附近发现错误语法错误。