I have the following table:
Date | Flag| Val | Weight
1 | A | 5 | 2
1 | B | 5 | 2
2 | A | 10 | 1
3 | A | 10 | 1
3 | B | 10 | 1
If I do SUM( val * weight ) / SUM( weight ) grouped by the Flag column, then I will get A: 7.5, B: 6.67. However, this is because the second date has no entry for B. I want to treat this as a zero value. So really I should get A: 7.5, B: 5.
How can I do this is SQL without modifying the table and adding a zero row for B?