Skip to content
Advertisement

Group elements of a column into mulitple subgroups SQL

I am looking at different breeds of cattle and their AnimalTypeCode , BreedCateoryID and resultant Growth.

I have the following query

This query is good and gives me almost what I want, but BreedCategoryID is numbered 1 through 7 and I would like to group them:

So instead of getting the mean Growthrate for each BreedCategoryID I would like to get the average for Pure Dairy, Dairy, and Beef.

Any help greatly appreciated!

Advertisement

Answer

You can assign a new “variable” using cross apply in the from clause:

Note that I also introduced table aliases and qualified all the column references.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement