Select and count with null values

advertisements

I have the following select:

SELECT
    COALESCE (opened.ano, closed.ano) AS ano,
    COALESCE (opened.mes, closed.mes) AS mes,
    COALESCE (opened.cnt, 0) AS opened_cases,
    COALESCE (closed.cnt, 0) AS closed_cases
FROM
    (
        SELECT
            YEAR (OPEN_DATE) AS ano,
            MONTH (OPEN_DATE) AS mes,
            COUNT (*) AS cnt
        FROM
            TABLE1,
            TABLE2
        WHERE
            TABLE1.USERNAME = TABLE2.USERNAME
        AND TABLE2.GROUP = 'SUPPORT'
        GROUP BY
            YEAR (OPEN_DATE),
            MONTH (OPEN_DATE)
    ) opened
FULL OUTER JOIN (
    SELECT
        YEAR (CLOSE_DATE) AS ano,
        MONTH (CLOSE_DATE) AS mes,
        COUNT (*) AS cnt
    FROM
        TABLE1,
        TABLE2
    WHERE
        TABLE1.USERNAME = TABLE2.USERNAME
    AND TABLE2.GROUP = 'SUPPORT'
    GROUP BY
        YEAR (CLOSE_DATE),
        MONTH (CLOSE_DATE)
) closed ON opened.ano = closed.ano
AND opened.mes = closed.mes
ORDER BY
    COALESCE (opened.ano, closed.ano) ASC,
    COALESCE (opened.mes, closed.mes) ASC;

The result is:

The situation:

The first line with null values are lost because there is no null condition in the select.

Thanks


select
  coalesce(opened.ano, closed.ano) as ano,
  coalesce(opened.mes, closed.mes) as mes,
  coalesce(opened.cnt, 0) as opened_cases,
  coalesce(closed.cnt, 0) as closed_cases
from
(
  select
    year(open_time) as ano,
    month(open_time) as mes,
    count(*) as cnt
  from table1
  where groupdesc = 'SUPPORT'
  group by year(open_time), month(open_time)
) opened
full outer join
(
  select
    year(close_time) as ano,
    month(close_time) as mes,
    count(*) as cnt
  from table1
  where groupdesc = 'SUPPORT'
  group by year(close_time), month(close_time)
) closed
  on opened.ano = closed.ano and opened.mes = closed.mes
where closed.mes is not null
order by coalesce(opened.ano, closed.ano) desc, coalesce(opened.mes, closed.mes)    desc;

This is your SQL with an added WHERE clause. Your comments seem to show that what you are looking for IS contained in this results set. There are 10 Opened_cases and 8 closed_cases.