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.

<!-- otherprops: set_clipboard -->
SELECT LeadSource, Rating,
    GROUPING(LeadSource) grpLS, GROUPING(Rating) grpRating,
    COUNT(Name) cnt
FROM Lead
GROUP BY ROLLUP(LeadSource, Rating)

For more details, see Identify Subtotals with GROUP BY.