SELECT
  U1.UserID,
  X.UserName,
  ROUND(X.HitCount + X.TotalWaitTime + X.AverageWaitTime * 20.0) AS WorkLoad,
  IF((X.TotalWaitTime > @nTotalWaitTimeThreshold) OR (X.AverageWaitTime > @nAverageWaitTimeThreshold), 0, 1) AS HappyFace
FROM (

  SELECT
    UserName,
    COUNT(*) AS HitCount,
    SUM(PageBuildSeconds) AS TotalWaitTime,
    AVG(PageBuildSeconds) AS AverageWaitTime
  FROM
    taPageHit
  WHERE
    (PageHitDateTime > DATE_SUB(NOW(), INTERVAL @nBlockCount MINUTE)) AND
    (UserName <> 'AUTO')
  GROUP BY
    UserName
  ORDER BY
    2 DESC
  LIMIT 30

) AS X INNER JOIN taUsers AS U1 ON (U1.FullName = X.UserName)
WHERE (U1.Status = 1)