Newer Version Available
GROUPING(fieldName)
You can use the GROUPING(fieldName) function in SELECT, HAVING, and ORDER BY clauses.
Use the GROUPING(fieldName) function to determine whether a row is a subtotal or field when you use GROUP BY ROLLUP or GROUP BY CUBE in SOQL queries.
GROUPING(fieldName) returns 1 if the row is a subtotal for the field, and 0 otherwise.
For more details, see Identify Subtotals with GROUP BY.