Skip to content
Advertisement

Performing a Union on groups of rows without the same columns. Trying to generate empty histogram bins

I am using MySQL.

I have a table grades, that looks like so:

I have a view that counts how many grades are in each bin, for each class using the following query.

This returns:

I want to add empty bins to this view, like so:

So the bins increment by 5, starting at 40, and stopping at 100. My thought was to make a table for empty bins, insert each grade bin and a count of 0, then use UNION but I can’t get the UNION to work, since I don’t have the course_id column in the empty bin table (nor do I want to – there are a lot and they could change). How would I do a UNION for each group of course_ids?

Advertisement

Answer

Assuming that you have created the table bin_table with only 1 column grade_bin and values 40, 45, 50, …., 100 and also that you have a table cources with the column course_id as primary key, all you have to do is CROSS join these tables and LEFT join to a query that aggregates on the table ranked_grades (no subquery needed):

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