Probably you get your result with a pivot table.