groupby for refs