You can do it without cycles
SELECT p.date, COALESCE(a.value, 0) value, p.grp_no FROM ( SELECT grp_no, date FROM ( SELECT grp_no, MIN(date) min_date, MAX(date) max_date FROM tableA GROUP BY grp_no ) q CROSS JOIN tableb b WHERE b.date BETWEEN q.min_date AND q.max_date ) p LEFT JOIN TableA a ON p.grp_no = a.grp_no AND p.date = a.date
The innermost subquery captures minutes and maximum numbers for each group. Then cross-connecting to TableB creates all possible dates in the min-max range for each group. Finally, the outer selection uses an outer join with TableA and populates the value 0 column for dates not in TableA .
Output:
| DATE | VALUE | GRP_NO |
| ------------ | ------- | -------- |
| 2012-08-06 | 1 | 1 |
| 2012-08-07 | 0 | 1 |
| 2012-08-08 | 1 | 1 |
| 2012-08-09 | 0 | 1 |
| 2012-08-07 | 2 | 2 |
| 2012-08-08 | 1 | 2 |
| 2012-08-09 | 0 | 2 |
| 2012-08-10 | 0 | 2 |
| 2012-08-11 | 0 | 2 |
| 2012-08-12 | 3 | 2 |
Here is the SQLFiddle demo
source share