答案 0 :(得分:1)
例如,您正在寻找PIVOT
MS SQL函数check this answer。
你可以不用PIVOT
这样做:
SELECT
p.PersonName,
SUM(CASE WHEN j.IdJob IS NULL THEN 0 ELSE 1 END ) [IT],
SUM(CASE WHEN j1.IdJob IS NULL THEN 0 ELSE 1 END ) [Teacher],
SUM(CASE WHEN j2.IdJob IS NULL THEN 0 ELSE 1 END ) [Doctor]
FROM Person p
LEFT OUTER JOIN Job j ON j.IdJob = p.IdJob AND j.JobName = 'IT'
LEFT OUTER JOIN Job j1 ON j1.IdJob = p.IdJob AND j1.JobName = 'Teacher'
LEFT OUTER JOIN Job j2 ON j2.IdJob = p.IdJob AND j2.JobName = 'Doctor'
GROUP BY p.PersonName
无论如何,没有简单明了的方法来制作PIVOT
表而不枚举所有列名。