由于针对特定候选人的反馈数量众多,我有一个重复值的数据集。
有3种反馈类型:“安全”,“其他”和“社交”。
候选人可能有多个反馈。并且此反馈类型名称必须通过将Org和orgtype表连接在一起来获取。但这给了我结果集中重复的值。
几列查询如下:
select
c.id as [Candidate ID]
,c.name as [Candidate Name]
,cf.status as [feedback status]
,e.name as [Feedback Type]
from
Candidates c
left join
Candidate_Feedback CF ON CF.CandidateId = c.ID
left join
Organizations d on CF.OrgId = d.ID
left join
OrganizationTypes e on d.OrganizationTypeId = e.Id
如何在每个候选人只需要一行的数据透视表和列而不是行的反馈类型中? (例如 col1-Feedback_social , Col2-Feedback_Other , col3-Feedback_Security ,col4-'N / A' (如果没有反馈)
由于查询中的其他情况,我需要为每位候选人提供这3列。
答案 0 :(得分:0)
您可以使用条件聚合,例如:
select
c.id as [Candidate ID],
c.name as [Candidate Name]
max(case when e.name = 'Social' then cf.status end) [Feedback Social],
max(case when e.name = 'Other' then cf.status end) [Feedback Other],
max(case when e.name = 'Security' then cf.status end) [Feedback Security]
from Candidates c
left join Candidate_Feedback CF ON CF.CandidateId = c.ID
left join Organizations d on CF.OrgId = d.ID
left join OrganizationTypes e on d.OrganizationTypeId = e.Id
group by c.id, c.name