Select 选择并使用空值计数

Select 选择并使用空值计数,select,sql-server-2008-r2,Select,Sql Server 2008 R2,我有以下选择: 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_DAT

我有以下选择:

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;
结果是:

情况:

由于select中没有null条件,因此具有null值的第一行丢失

谢谢

这是添加了WHERE子句的SQL。 您的评论似乎表明您正在寻找的内容包含在此结果集中。
有10个打开的箱子和8个关闭的箱子。

所以在你的小提琴手例子中应该有10个打开的箱子和8个关闭的箱子

是的,那么试试这个:()


您确定应该将它们添加到该行吗?这些行没有
CLOSE\u DATE
,但重要的是,它们可能已经在
OPEN\u DATE
所指的适当时间段内被计算在内。我原以为把它们加进去会导致重复计算。@Damien_不信者,你说得对。这些行正在统计中,4个案例中有3个在4月开放,1个在5月开放,所有4个案例仍然开放。因此,是否可以将select更改为:如果关闭日期为空,则从打开日期获取年份和月份,并添加1。谢谢。我原以为正确的数据修复方法只是为
closed
查询提供一个额外的过滤器,其中
CLOSE\u DATE
不是空的。你仍然在谈论在其他地方添加随机数,这仍然是错误的。抱歉@Damien_,不信者,你仍然是对的。select中空值的筛选器应该修复它。再次感谢您,您想让我更改ON条款吗?我在(opened.ano=closed.ano)或(opened.ano为null且closed.ano为null)和(opened.mes=closed.mes)或(opened.mes为null且closed.mes为null)上更改了此
但不起作用。请解释您试图实现的目标。当您说“由于select中没有null条件,所以具有null值的第一行丢失。”这一行不会像在结果集中一样丢失。是因为您有一个数据行的开放日期为空,而关闭日期为空,所以您希望看到结果中显示的结果吗?你的问题不清楚。另外,了解哪个表有打开和关闭日期也会有所帮助。也许您可以使用表1和表2的表结构进行注释。例如:如果票据没有关闭日期,因此为空值,则表示票据已打开,因此应计为打开。这是select没有执行的操作。此线程在开始时对我帮助很大:。存在一些sqlfiddle。正在发生的情况如下:。这两个案例应属于开放时间起的年度和月份的开放案例。2014/01年度为1,2013/02年度为其他
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;
;
with opened AS (
  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)
  ),
closed AS (
  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)
)
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 opened
FULL OUTER JOIN closed on opened.ano = closed.ano and opened.mes = closed.mes
WHERE COALESCE (opened.ano, closed.ano) IS NOT NULL
ORDER BY coalesce(opened.ano, closed.ano) desc, coalesce(opened.mes, closed.mes) desc;