我有一个相当复杂的 PIVOT 查询,可能应该分成几部分。我的问题是我最终需要删除重复的行。
SELECT
PostName,
ActivityID,
ProjectName,
ProjectID,
ObjectiveID,
ObjectiveName,
LastName,
FirstName,
VolID,
Orgs,
Coms,
ISNULL([Males 0-9], 0) [Males 0-9],
ISNULL([Males 10-17], 0) [Males 10-17],
ISNULL([Males 18-24], 0) [Males 18-24],
ISNULL([Males 25+], 0) [Males 25+],
ISNULL([Females 0-9], 0) [Females 0-9],
ISNULL([Females 10-17], 0) [Females 10-17],
ISNULL([Females 18-24], 0) [Females 18-24],
ISNULL([Females 25+], 0) [Females 25+]
FROM
(
SELECT
a.PostName,
a.ActivityID,
pr.PostID,
pr.ProjectName,
pr.ProjectID,
o.ObjectiveID,
o.ObjectiveName,
a.LastName,
a.FirstName,
a.VolID,
ISNULL(COUNT(DISTINCT ac1.ActivityCollaboratorID), 0) AS Orgs,
ISNULL(COUNT(DISTINCT ac2.ActivityCollaboratorID), 0) AS Coms,
p.AddInd,
b.Name
FROM
[PCVRT].[dbo].ActivityObjectives ao
LEFT JOIN [PCVRT].[dbo].[ReportCSPPView] a
ON ao.ActivityID = a.ActivityID
LEFT JOIN [PCVRT].[dbo].[ActivityParticipants] p
ON a.ActivityID = p.ActivityID
AND a.GroupName IS NOT NULL
AND a.MMED <> 1
FULL OUTER JOIN [PCVRT].[dbo].Buckets b
ON
b.BucketID = p.BucketID
AND b.BucketSetID =
(
SELECT BucketSetID FROM [PCVRT].[dbo].BucketSets
WHERE Name = 'Standard'
)
LEFT JOIN PCVRT.dbo.Objectives o
ON o.ObjectiveID = ao.ObjectiveID
LEFT JOIN PCVRT.dbo.ActivityCollaborators ac1
ON a.ActivityID = ac1.ActivityID
AND ac1.CollaboratorTypeID = 1
LEFT JOIN PCVRT.dbo.ActivityCollaborators ac2
ON a.ActivityID = ac2.ActivityID
AND ac2.CollaboratorTypeID = 2
LEFT JOIN PCVRT.dbo.Projects pr
ON pr.ProjectID = a.ProjectId
GROUP BY
a.ActivityID,
a.PostName,
pr.PostID,
pr.ProjectName,
pr.ProjectID,
o.ObjectiveID,
o.ObjectiveName,
a.LastName,
a.FirstName,
a.VolID,
p.AddInd,
b.Name
) AS source
PIVOT
(
MAX(AddInd)
FOR Name IN([Males 0-9], [Males 10-17], [Males 18-24], [Males 25+], [Females 0-9], [Females 10-17], [Females 18-24], [Females 25+])
) AS pvt
WHERE ProjectName IS NOT NULL
AND ObjectiveName IS NOT NULL
ORDER BY
ProjectID,
ObjectiveID,
ProjectName,
ObjectiveName,
LastName,
FirstName
所以,这样的结果是这样的:
+----------+--------------------------------------+---------------------------------------------------------------+--------------------------------------+--------------------------------------+--------------------------------------------------------------------+----------+-----------+-----------+------+------+----------+------------+------------+----------+------------+--------------+--------------+------------+
| PostName | ActivityID | ProjectName | ProjectID | ObjectiveID | ObjectiveName | LastName | FirstName | VolID | Orgs | Coms | Males0-9 | Males10-17 | Males18-24 | Males25+ | Females0-9 | Females10-17 | Females18-24 | Females25+ |
+----------+--------------------------------------+---------------------------------------------------------------+--------------------------------------+--------------------------------------+--------------------------------------------------------------------+----------+-----------+-----------+------+------+----------+------------+------------+----------+------------+--------------+--------------+------------+
| Zomba | 1432BB61-6DC7-4EB9-B7F1-08D3BD1500ED | Youth in Development Thematic | 663E5793-D40D-4543-AA63-287AF32567E6 | 1FD82DEC-A8C3-4679-9CA2-66E8977B6A30 | Community Engagement in English | Amadou | Daniel | 100582935 | 1 | 1 | 15 | 22 | 12 | 0 | 32 | 22 | 12 | 0 |
| Zomba | 1432BB61-6DC7-4EB9-B7F1-08D3BD1500ED | Youth in Development Thematic | 663E5793-D40D-4543-AA63-287AF32567E6 | 1E4EDF72-A13A-4BCB-9C78-6A879F512A44 | Community Engagement in Literacy | Amadou | Daniel | 100582935 | 1 | 1 | 15 | 22 | 12 | 0 | 32 | 22 | 12 | 0 |
| Zomba | 1432BB61-6DC7-4EB9-B7F1-08D3BD1500ED | Youth in Development Thematic | 663E5793-D40D-4543-AA63-287AF32567E6 | ACCAF29C-C2C8-488E-9406-6BE5FF6D1961 | Promoting Gender Equitable Practices | Amadou | Daniel | 100582935 | 1 | 1 | 15 | 22 | 12 | 0 | 32 | 22 | 12 | 0 |
| Zomba | 1432BB61-6DC7-4EB9-B7F1-08D3BD1500ED | Youth in Development Thematic | 663E5793-D40D-4543-AA63-287AF32567E6 | 819966D1-1D0E-4889-AE2F-72E0CDACF850 | Student-friendly Schools | Amadou | Daniel | 100582935 | 1 | 1 | 15 | 22 | 12 | 0 | 32 | 22 | 12 | 0 |
| Zomba | 1CC95367-D9EE-46ED-981E-74D3E944BBE8 | Project Test - Youth in Development Thematic | 543E1BF9-E17B-47AF-AE06-4C14EE494BA6 | E36000FC-BAE1-492B-823E-6F3441083727 | Increasing life skills and health education for children and youth | Allen | Touoze | 100343614 | 1 | 1 | 0 | 22 | 15 | 12 | 0 | 26 | 13 | 8 |
| Zomba | 3913846E-3973-4404-B3CA-D3ECFE357C00 | Project Test - Youth in Development Thematic | 543E1BF9-E17B-47AF-AE06-4C14EE494BA6 | E36000FC-BAE1-492B-823E-6F3441083727 | Increasing life skills and health education for children and youth | Allen | Touoze | 100343614 | 1 | 2 | 0 | 2 | 0 | 0 | 0 | 12 | 2 | 0 |
| Zomba | 1432BB61-6DC7-4EB9-B7F1-08D3BD1500ED | Secondary / Community Activities - Education Sector Framework | 06C877D7-317B-432C-B038-A548AC7BB59F | 1FD82DEC-A8C3-4679-9CA2-66E8977B6A30 | Community Engagement in English | Amadou | Daniel | 100582935 | 1 | 1 | 15 | 22 | 12 | 0 | 32 | 22 | 12 | 0 |
| Zomba | 1432BB61-6DC7-4EB9-B7F1-08D3BD1500ED | Secondary / Community Activities - Education Sector Framework | 06C877D7-317B-432C-B038-A548AC7BB59F | 1E4EDF72-A13A-4BCB-9C78-6A879F512A44 | Community Engagement in Literacy | Amadou | Daniel | 100582935 | 1 | 1 | 15 | 22 | 12 | 0 | 32 | 22 | 12 | 0 |
| Zomba | 1432BB61-6DC7-4EB9-B7F1-08D3BD1500ED | Secondary / Community Activities - Education Sector Framework | 06C877D7-317B-432C-B038-A548AC7BB59F | ACCAF29C-C2C8-488E-9406-6BE5FF6D1961 | Promoting Gender Equitable Practices | Amadou | Daniel | 100582935 | 1 | 1 | 15 | 22 | 12 | 0 | 32 | 22 | 12 | 0 |
| Zomba | 1432BB61-6DC7-4EB9-B7F1-08D3BD1500ED | Secondary / Community Activities - Education Sector Framework | 06C877D7-317B-432C-B038-A548AC7BB59F | 819966D1-1D0E-4889-AE2F-72E0CDACF850 | Student-friendly Schools | Amadou | Daniel | 100582935 | 1 | 1 | 15 | 22 | 12 | 0 | 32 | 22 | 12 | 0 |
+----------+--------------------------------------+---------------------------------------------------------------+--------------------------------------+--------------------------------------+--------------------------------------------------------------------+----------+-----------+-----------+------+------+----------+------------+------------+----------+------------+--------------+--------------+------------+
所以,我需要的是每个项目-> 目标配对只获得一行,汇总活动。
想法?