0
votes

I have Table with columns: "Month" and "Year", and other data.

All row in Table have different values "Month" and "Year".

But for some Month and Year rows don't exist.

I want create SQL-query (... where year in (2010, 2011, 2012) ...), that in result this SQL-query have all Month for select Year and if some month don't exist else add it to result with 0 in other data columns.

Example: Input: Table

data / month / year
+-----+---+------+
| 3.0 | 1 | 2011 |
| 4.3 | 3 | 2011 |
| 5.7 | 4 | 2011 |
| 2.2 | 5 | 2011 |
| 5.4 | 7 | 2011 |
+-----+---+------+

Output: SELECT ... WHERE year IN (2011)

+-----+----+------+
| 3.0 |  1 | 2011 |
|   0 |  2 | 2011 |
| 4.3 |  3 | 2011 |
| 5.7 |  4 | 2011 |
| 2.2 |  5 | 2011 |
|   0 |  6 | 2011 |
| 5.4 |  7 | 2011 |
|   0 |  8 | 2011 |
|   0 |  9 | 2011 |
|   0 | 10 | 2011 |
|   0 | 11 | 2011 |
|   0 | 12 | 2011 |
+-----+----+------+
3

3 Answers

3
votes

Try Partition Outer Join:

SELECT
  NVL(T.DATA, 0) DATA,
  F.MONTH,
  T.YEAR
FROM <your_table> T
PARTITION BY(T.YEAR)
RIGHT JOIN (SELECT LEVEL MONTH FROM DUAL CONNECT BY LEVEL <= 12) F ON T.MONTH = F.MONTH

Add your WHERE clause at the end or create a view with that definition and query against it.

2
votes
select datecol, 
       nvl(val,0),
       to_char(d.date_col,'MM') month,
       to_char(d.date_col,'yyyy') year
from(
    select add_months('1-Jan-2011',level-1) as datecol
    from dual connect by level <= 12
     ) d
left join(
    select sum(val) as val, month, year
    from your_table
    group by month, year
    ) S
on (to_char(d.date_col,'MM') = s.month and to_char(d.date_col,'yyyy') = s.year)
0
votes
select nvl(t.data, 0), x.month, nvl(t.year, <your_year>) as year
  from <your_table> t,
       (select rownum as month from dual connect by level < 13) x
 where (t.year is null or t.year = <your_year>)
   and t.month(+) = x.month
 order by x.month