PostgreSQL 14
Assuming this sample data:
| fruit | ripeness |
|---|---|
| orange | 1 |
| orange | 3 |
| apple | 0 |
| apple | 3 |
| apple | 3 |
| apple | 2 |
Is there a way to get ROLLUP to give me a count of fruits by ripeness with a row for each possible value of ripeness like this:
| fruit | ripeness | count |
|---|---|---|
| orange | 0 | 0 |
| orange | 1 | 1 |
| orange | 2 | 0 |
| orange | 3 | 1 |
| orange | 2 | |
| apple | 0 | 1 |
| apple | 1 | 0 |
| apple | 2 | 1 |
| apple | 3 | 2 |
| apple | 4 | |
| 6 |
Instead of only the existing values in the group appearing like this:
| fruit | ripeness | count |
|---|---|---|
| orange | 1 | 1 |
| orange | 3 | 1 |
| orange | 2 | |
| apple | 0 | 1 |
| apple | 2 | 1 |
| apple | 3 | 2 |
| apple | 4 | |
| 6 |