That script is very close to what I do with Oracle and SQL Server. I would make a simple change though. The two "CASE WHEN ISNULL()" should be "CASE WHEN GROUPING()", as this handles the situation where some of the values may be NULL and is a true indication of grouping.