Skip to content
Advertisement

How to add a category to the results of a query based on the output?

Extending this question for a more general case:

I have a table called fruits with a column fruit that has three possible values: apple, orange, and mango. I used the following query to get the counts of each fruit by date:

that gives the following results:

As you can see, not every date contains each type of fruit. Is it possible, and if so, how, to add the missing fruit by each date to the above results with a default count=0, as shown below?

Advertisement

Answer

Use a cross join ot generate the row and then a left join to bring in the results:

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