O'Reilly logo
  • Dustin Waybright thinks this is interesting:

The GROUPING function accepts a single element as input and returns 0 when the element is part of the grouping set and 1 when it isn’t. The following query demonstrates using the GROUPING function.

  shipcountry, GROUPING(shipcountry) AS grpcountry,
  shipregion , GROUPING(shipregion) AS grpregion,
  shipcity   , GROUPING(shipcity) AS grpcity,
  COUNT(*) AS numorders
FROM Sales.Orders
GROUP BY ROLLUP( shipcountry, shipregion, shipcity );


Cover of Training Kit (Exam 70-461): Querying Microsoft® SQL Server® 2012


I this case these extra columns are added to the output and treated by T-SQL like aggregates, meaning they don't have to be in the GROUP clause.