Please help optimize this query:
SELECT ts.SiteId, COUNT(ts.SiteId) AS Count
FROM ts
WHERE ts.SiteId not in
(SELECT ts.SiteId FROM ts WHERE ts.uniqueid = 'xxx')
GROUP BY ts.SiteId ORDER BY Count DESC
SELECT ts.SiteId, COUNT(ts.SiteId) AS Count,
MAX(CASE WHEN ts.uniqueid = 'xxx' THEN 1 ELSE 0 END) As XXXUniqueID
FROM ts
GROUP BY ts.SiteId
HAVING XXXUniqueID = 0
ORDER BY Count DESC
MAX(...) expression into the HAVING clause instead.