In example 2, couldn't this be used also?
DECLARE @orderby int
SET @orderby = 1
FROM #Locations l
INNER JOIN #Users u ON l.LocationID = u.LocationID
INNER JOIN #Components c ON c.UserID = u.UserID
GROUP By l.State, ComponentName
ORDER BY CASE @orderby
WHEN 1 THEN l.State
WHEN 2 THEN ComponentName
END
I do like the changing of the order by with a variable. Definitely a nice little trick.
Pat
Pat Buskey