I have a couple of "disaster queries" with that requirement. I build them up with lots of interpolation so that as conditions change, I am sure that they change the same way in the WHERE, a CASE statement in the SELECT, and the corresponding CASE statement in the GROUP BY. :-(
Cheers,
Ben