Q&A for professional and enthusiast programmers
with tagged as ( select PostId, case when TagId = 5 then 1 else null end as Opt, case when TagId = 241 then 1 else null end as Perf from PostTags ), perfopt as ( select PostId, Max(Opt) as Opt, Max(Perf) as Perf from tagged where Perf = 1 OR Opt = 1 group by PostId ) Select Count (Opt) as Opt, Count(Perf) as Perf, Count(case when Opt = 1 and Perf = 1 then 1 else null end) as Both from perfopt