Consider a table showing various fruit and the quantity that they exist in:
x---------x-----x
| FRUIT | QTY |
x---------x-----x
| Apple | 4 |
| Orange | 5 |
| Mango | 4 |
| Grape | 1 |
| Plum | 2 |
| Peach | 2 |
x---------x-----x
From this table I want to query the number of fruit (ie. count the number of records) that have a specific quantity starting from 0 and ending at MAX(QTY), so that my result set would be:
x-----x-------x
| QTY | COUNT |
x-----x-------x
| 0 | 0 | //0 fruits have 0 quantity
| 1 | 1 | //1 fruit (Grape) has 1 quantity
| 2 | 2 | //2 fruits (Plum, Peach) have 2 quantity
| 3 | 0 | //0 fruits have 3 quantity
| 4 | 2 | //2 fruits (Apple, Mango) have 4 quantity
| 5 | 1 | //1 fruit (Orange) has 5 quantity
x-----x-------x
How can this be achieved?