I have a sales table that records sale of an item. simplified version is below
| ID | ItemID | SaleTime |
|---|---|---|
| 1 | 1234 | 2020-12-01 12:44:22 |
| 2 | 1234 | 2020-12-01 17:12:22 |
| 3 | 1234 | 2020-12-02 12:44:22 |
| 4 | 1234 | 2020-12-04 17:12:22 |
i am writing a query to count items sold each day which is working fine and giving following results.
| ID | ItemID | Date | Sale count |
|---|---|---|---|
| 1 | 1234 | 2020-12-01 | 2 |
| 2 | 1234 | 2020-12-02 | 1 |
| 3 | 1234 | 2020-12-04 | 1 |
how do I include days where no sale was made with zero count like following.
| ID | ItemID | Date | Sale count |
|---|---|---|---|
| 1 | 1234 | 2020-12-01 | 2 |
| 2 | 1234 | 2020-12-02 | 1 |
| 3 | 1234 | 2020-12-03 | 0 |
| 4 | 1234 | 2020-12-04 | 1 |
ìdat all as technical keys often are more confusing than informational in the result. Letitem_idbe your first column and sort your query by that first, then bydate. Be extra careful on days where no samples of a certain item got sold as there can benullvalues which can let you miss some rows of your result!left joinin combination with thecoalesce()-function could do a simple job for you, but i'm currently looking up for that myself