Pivot,Unpivot,Cross Apply?

时间:2015-04-28 14:56:44

标签: sql-server pivot

说我有类似下面的数据和我真正想要的是(枢轴?)AssigedType作为列(处理,监督等)虽然我不认为枢轴是正确的方式,因为我不是汇总任何东西。将涉及三个表:事项,事项专业人员,专业人员),即

Matters.Matters = MattersProfessionals.Matters Professionals.Professionals = Matters.Professionals

matterid    AssignedType    ProfName
27391-0001001   Handling    Skip A. Lawyer
37085-0051001   Handling    Skip A. Lawyer
37085-0051001   Supervising Skip A. Lawyer
18814-0226WO1   Handling    Skip A. Lawyer
37085-0050001   Handling    Skip A. Lawyer
37085-0050001   Supervising Skip A. Lawyer
37085-0053001   Supervising Skip A. Lawyer
37085-0053001   Handling    Skip A. Lawyer
37085-0052001   Handling    Skip A. Lawyer
37085-0052001   Supervising Skip A. Lawyer
18814-0226AU1   Handling    Skip A. Lawyer

期望的输出:

MatterID       Handling        Supervising 
27391-0001001  Skip A. Lawyer  Skip A.Lawyer 
etc...

像枢轴但没有聚合?

1 个答案:

答案 0 :(得分:0)

Min()或Max()也可以是您的枢轴功能的聚合:

SELECT * from (<here the original select goes>) 
AS source
PIVOT 
    (min(ProfName) For AssignedType in ([Handling], [Supervising])) 
AS y;

这会返回一个像

这样的表格
matterid       Handling        Supervising     
-------------  --------------  --------------  
27391-0001001  Skip A. Lawyer  (null)          
37085-0051001  Skip A. Lawyer  Skip A. Lawyer  
...