Skip to content
Advertisement

SQL query with avg and group by

I have some problems with writing a SQL query for MySQL. I have a table with the following structure:

I want to create a query that extracts the following information from the table:

The result of the query should look like this:

with more rows for each unique ‘id’, of course.

I already tried some queries like

This returns the correct result, but I have to expand it with results for the other possible values of ‘pass’ (up to 7)

I tried to use a nested SELECT within AVG but this didn’t work because I didn’t figure out how to correctly limit it to the current ‘id’.

I then created Views to represent the result of each query for ‘pass’ = 1, ‘pass’ = 2, etc. But for most ids the highest value for ‘pass’ is 5. When using JOIN queries to get the final result from the views I received an empty result set, because some of the Views are empty / don’t have values for a specific ‘id’.

Any ideas?

Advertisement

Answer

If I understand what you need, try this:

Or, if you want just one row for every id, this:

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