在PIVOT中使用两列

时间:2015-07-31 12:24:32

标签: sql-server tsql pivot

我已经发布了这个问题Use many to many relation to generate columns like a pivot并得到了答案,但现在我需要在结果集中再做一件事。

我的User表格有一个Register列,其中包含公司人力资源部门的用户ID。当用户有Register时,我需要返回此MemberType = 0列。我怎么能这样做?

以下是创建示例数据库的脚本:

CREATE TABLE Project (
  Id int,
  Name VARCHAR(50),
  CONSTRAINT PK_Project PRIMARY KEY (Id));


CREATE TABLE [User] (
  Id int,
  Name VARCHAR(50),
  Register int,
  CONSTRAINT PK_User PRIMARY KEY (Id));

CREATE TABLE ProjectMember (
  Project_Id int,
  User_Id int, 
  MemberType tinyint,
  CONSTRAINT PK_ProjectMember PRIMARY KEY(Project_Id, User_Id),
  CONSTRAINT FK_ProjectMember_Project FOREIGN KEY(Project_Id) REFERENCES Project(Id),
  CONSTRAINT FK_ProjectMember_User FOREIGN KEY(User_Id) REFERENCES [User](Id));



 INSERT INTO Project(Id, Name) VALUES(1, 'Project 1');
 INSERT INTO Project(Id, Name) VALUES(2, 'Project 2');
 INSERT INTO Project(Id, Name) VALUES(3, 'Project 3');
 INSERT INTO Project(Id, Name) VALUES(4, 'Project 4');
 INSERT INTO Project(Id, Name) VALUES(5, 'Project 5');
 INSERT INTO Project(Id, Name) VALUES(6, 'Project 6');
 INSERT INTO Project(Id, Name) VALUES(7, 'Project 7');


 INSERT INTO [User](Id, Name, Register) VALUES(1, 'User 1', 23498374);
 INSERT INTO [User](Id, Name, Register) VALUES(2, 'User 2', 96849887);
 INSERT INTO [User](Id, Name, Register) VALUES(3, 'User 3', 6546884);
 INSERT INTO [User](Id, Name, Register) VALUES(4, 'User 4', 8489848);
 INSERT INTO [User](Id, Name, Register) VALUES(5, 'User 5', 4684854);
 INSERT INTO [User](Id, Name, Register) VALUES(6, 'User 6', 4849888);
 INSERT INTO [User](Id, Name, Register) VALUES(7, 'User 7', 84884446);
 INSERT INTO [User](Id, Name, Register) VALUES(8, 'User 8', 77554454);
 INSERT INTO [User](Id, Name, Register) VALUES(9, 'User 9', 77853997);

 INSERT INTO ProjectMember(Project_Id, User_Id, MemberType) VALUES(1, 1, 0);
 INSERT INTO ProjectMember(Project_Id, User_Id, MemberType) VALUES(1, 2, 1);
 INSERT INTO ProjectMember(Project_Id, User_Id, MemberType) VALUES(1, 3, 2);
 INSERT INTO ProjectMember(Project_Id, User_Id, MemberType) VALUES(1, 4, 2);
 INSERT INTO ProjectMember(Project_Id, User_Id, MemberType) VALUES(1, 5, 2);
 INSERT INTO ProjectMember(Project_Id, User_Id, MemberType) VALUES(3, 6, 0);
 INSERT INTO ProjectMember(Project_Id, User_Id, MemberType) VALUES(3, 5, 1);
 INSERT INTO ProjectMember(Project_Id, User_Id, MemberType) VALUES(4, 3, 0);
 INSERT INTO ProjectMember(Project_Id, User_Id, MemberType) VALUES(4, 4, 1);
 INSERT INTO ProjectMember(Project_Id, User_Id, MemberType) VALUES(4, 5, 2);

 INSERT INTO ProjectMember(Project_Id, User_Id, MemberType) VALUES(5, 7, 0);

 INSERT INTO ProjectMember(Project_Id, User_Id, MemberType) VALUES(6, 8, 1);

我必须返回这样的数据:

ProjectId | ProjectName | UserType0 (Name) | UserType0 (Register) | UserType1 (Name)

1 个答案:

答案 0 :(得分:2)

我会考虑使用条件聚合方法,而不是使用pivot运算符。

我认为这个查询应该做你想做的事情:

SELECT  
    ProjectID              = P.Id, 
    ProjectName            = P.Name, 
    [UserType0 (Name)]     = MAX(CASE WHEN MemberType = 0 THEN u.Name END),
    [UserType0 (Register)] = MAX(CASE WHEN MemberType = 0 THEN Register END), 
    [UserType1 (Name)]     = MAX(CASE WHEN MemberType = 1 THEN u.Name END) 
FROM Project AS P
LEFT JOIN ProjectMember AS PM ON P.Id = PM.Project_Id
LEFT JOIN [User] AS U ON PM.User_Id = U.Id 
GROUP BY P.Id, P.Name

使用您的样本数据,结果将是:

ProjectID   ProjectName UserType0 (Name)    UserType0 (Register)    UserType1 (Name)
1           Project 1   User 1              23498374                User 2
2           Project 2   NULL                NULL                    NULL
3           Project 3   User 6              4849888                 User 5
4           Project 4   User 3              6546884                 User 4
5           Project 5   User 7              84884446                NULL
6           Project 6   NULL                NULL                    User 8
7           Project 7   NULL                NULL                    NULL