Skip to content
Advertisement

SQL: Retrieve missing values from superset

I have a table like this:

I want to return a table with all the ratings possible. In this case 1 to 5. So my resulting table will be:

I have tried using something like:

but this does not work. Can you please suggest a solution for this?

Advertisement

Answer

You can cross join the list of distinct devices with numbers between 1 and 5:

Or if you want to generate the rating range from the minium and maximum values that are in the table:

Finally: if all the ratings are availabel in the table already, no need to use generate_series():

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