Suppose I have table Tab (id,, DeptName, Total, ...)
There are 4 possible values for Deptname: Dept1, Dept2, Dept3, Dept4.
Then I can issue SQL for grouping like this:
Select DeptName, Totaol = Sum(total)
from Tab
group by DeptName
Normally, the result is will have 4 rows based on the value of DeptName:
DeptName Total
Dept1 1234.09
Dept2 234.80
Dept3 34.34
Dept4 614.48
If there is no data for Dept2, the result will only has 3 rows:
Dept1 1234.09
Dept3 34.34
Dept4 614.48
What I want is the result always has 4 rows. If there is no data for Dept2, I want the result like:
Dept1 1234.09
Dept2 0
Dept3 34.34
Dept4 614.48
How to implement this request?
This seems right SQL Fiddle: