Apply group by statements