【问题标题】:Select and Count with Null values使用 Null 值选择和计数
【发布时间】:2014-05-06 21:12:15
【问题描述】:

我有以下选择:

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;

结果是:

情况:

第一行有空值的行丢失了,因为选择中没有空条件。

谢谢

【问题讨论】:

  • 您确定应该将它们添加到该行吗?这些是没有CLOSE_DATE 的行,但重要的是,它们可能已经被计入OPEN_DATE 所指的适当时期。我原以为添加它们会导致重复计算它们。
  • @Damien_The_Unbeliever,你是对的。这些行正在统计中,4 起案件中有 3 起在 4 月份开立,5 月份开立 1 起,所有 4 起案件仍然开庭。那么是否可以将选择更改为:如果 closed_date 为空,则从 opens_date 获取年份和月份并添加 1。谢谢。
  • 我原以为对您的数据的正确修复只是作为closed 查询的额外过滤器,其中CLOSE_DATE 不为空。您仍然在谈论在其他地方添加随机数仍然感觉不对。
  • 对不起@Damien_The_Unbeliever,你还是对的。选择中的空值过滤器应该修复它。再次感谢

标签: select sql-server-2008-r2


【解决方案1】:
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;

这是添加了 WHERE 子句的 SQL。 您的 cmets 似乎表明您正在寻找的内容包含在此结果集中。 有 10 个 Opened_cases 和 8 个 closed_cases。

【讨论】:

  • 嗨,你想让我更改ON 子句吗?我改成了这个ON (opened.ano = closed.ano) or (opened.ano is null and closed.ano is null) AND (opened.mes = closed.mes) or (opened.mes is null and closed.mes is null),但是没有用。
  • 请解释您要完成的工作。当您说“由于选择中没有空条件而丢失了具有空值的第一行”。此行不会丢失,因为它在结果集中。是因为您有一个数据行的 OPEN_DATE 和 NULL CLOSE_DATE,您希望在结果中看到该显示吗?你的问题不清楚。还知道哪个表有 OPEN_DATE 和 CLOSE_DATE 会有所帮助。或许您可以评论一下 Table1 和 Table2 的表结构。
  • 一个例子:如果一个工单没有close_date,因此一个空值,这意味着它是开放的,所以它应该算作开放。这是 select 没有做的事情。
  • 这个帖子一开始对我有很大帮助:stackoverflow.com/questions/23012547/…。有一些sqlfiddle。
  • 这就是正在发生的事情:sqlfiddle.com/#!3/3bf0ac/1。 2 个案例应该属于从 open_time 开始的年份和月份的未结案例。 1 用于 2014/01,其他用于 2013/02
【解决方案2】:

所以在您的提琴手示例中应该有 10 个打开的案例和 8 个关闭的案例?

是这样,那就试试这个:(http://sqlfiddle.com/#!3/3bf0ac/9)

;
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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    相关资源
    最近更新 更多