Fix issue 5968

Added a real groupBy facility. Making it single-pass was interesting.