Skip to content
Advertisement

How to query the percent of totals in a group by query

I have a query that pulls the total number of records grouped by the year and length of characters in the identification number. I want to add a column that calculates the percentage of this total against the total for each record year.

This is the query that I am using, and so far I’m able to pull the year, ID length, and the number of records; however, I’m unable to pull the percentage of the total, grouping by the year:

This is what I want the final table to look like:

Is there a way I can accomplish this in one query? Thank you for looking!

Advertisement

Answer

If window functions are available you can do this:

Otherwise a subquery is needed (where clause, if any, must match):

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