You can join the table against yourself:
select t.jobId, t.jobName, count(p1.jobId) as Priority1, count(p2.jobId) as Priority2, count(p3.jobId) as Priority3, count(p4.jobId) as Priority4, count(p5.jobId) as Priority5 from theTable t left join theTable p1 on p1.jobId = t.jobId and p1.jobName = t.jobName and p1.Priority = 1 left join theTable p2 on p2.jobId = t.jobId and p2.jobName = t.jobName and p2.Priority = 2 left join theTable p3 on p3.jobId = t.jobId and p3.jobName = t.jobName and p3.Priority = 3 left join theTable p4 on p4.jobId = t.jobId and p4.jobName = t.jobName and p4.Priority = 4 left join theTable p5 on p5.jobId = t.jobId and p5.jobName = t.jobName and p5.Priority = 5 group by t.jobId, t.jobName
Or you can use the case inside the sum:
select jobId, jobName, sum(case Priority when 1 then 1 else 0 end) as Priority1, sum(case Priority when 2 then 1 else 0 end) as Priority2, sum(case Priority when 3 then 1 else 0 end) as Priority3, sum(case Priority when 4 then 1 else 0 end) as Priority4, sum(case Priority when 5 then 1 else 0 end) as Priority5 from theTable group by jobId, jobName
Guffa Aug 17 '09 at 14:09 2009-08-17 14:09
source share