I have a SQL query like this:
SET @q =
(SELECT Id AS '@Id', COUNT(Occ) AS '@Occ' FROM Details
GROUP BY Id
ORDER BY COUNT(Occ) DESC, Id ASC
FOR XML PATH('Data'), ROOT('Stats'), ELEMENTS, TYPE)
I'm setting AS @Id and AS @Occ in order for my FOR XML directive to transform the output as attributes instead of elements.
My question: Does the double occurrence of COUNT(Occ) both in the SELECT and in the ORDER BY cause the count to be executed twice, and if so, how can I prevent this from happening?
Thanks!