This is the current situation:
Table1
| key | some_id | date | class |
|---|---|---|---|
| 1 | 1 | 1.1.2000 | 2 |
| 1 | 2 | 1.1.2000 | 2 |
| 2 | 1 | 1.1.1999 | 3 |
| ... | ... | ... | ... |
I'm counting the classes and providing the information through a view by using following select statement:
SELECT key, date, class, count(class) as cnt
FROM table1
GROUP BY key, date, class
The result would be:
| key | date | class | cnt |
|---|---|---|---|
| 1 | 1.1.2000 | 2 | 2 |
| 2 | 1.1.1999 | 3 | 1 |
| ... | ... | ... | ... |
but now there is another table which includes all possible class-codes, e.g.
| parameter_key | class_code |
|---|---|
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
| ... | ... |
For my view I'm only querying data for parameter_key 1. And the view now needs to show all possible class_codes, also if the count would be 0.
So my desired result table is:
| key | date | class | cnt |
|---|---|---|---|
| 1 | 1.1.2000 | 1 | 0 |
| 1 | 1.1.2000 | 2 | 2 |
| 1 | 1.1.2000 | 3 | 0 |
| 2 | 1.1.1999 | 1 | 0 |
| 2 | 1.1.1999 | 2 | 0 |
| 2 | 1.1.1999 | 3 | 1 |
| ... | ... | ... | ... |
but I just can't get my head around how to do this. I've tried to add a right join like this but that does not change anything (probably because I join the class column and do an aggregate which won't be displayed if there is nothing to count?):
SELECT key, date, class, count(class) as cnt
FROM table1
RIGHT JOIN table2 on table1.class = table2.class and table2.parameter_key = 1
GROUP BY key, date, class
Any idea on how to achieve the desired result table?