"IS NOT NULL" 未在 mySQL 中产生所需的结果

"IS NOT NULL" not producing the required results in mySQL

我正在尝试根据我的 table 构建报告。这是我的 table:


以下是获得所需结果的SQL查询。

SELECT
  `user`,
  SUM(
    CASE
      WHEN `event_time` >= '2021-04-01 00:00:00'
      AND `event_time` <= '2021-04-16 23:59:59'
      AND `sub_status` = 'TB'
      THEN pause_sec
    END
  ) AS 'Training Break',
  SUM(
    CASE
      WHEN `event_time` >= '2021-04-01 00:00:00'
      AND `event_time` <= '2021-04-16 23:59:59'
      AND `sub_status` = 'SB1'
      THEN pause_sec
    END
  ) AS 'Short Break 1',
  SUM(
    CASE
      WHEN `event_time` >= '2021-04-01 00:00:00'
      AND `event_time` <= '2021-04-16 23:59:59'
      AND `sub_status` = 'SB2'
      THEN pause_sec
    END
  ) AS 'Short Break 2',
  SUM(
    CASE
      WHEN `event_time` >= '2021-04-01 00:00:00'
      AND `event_time` <= '2021-04-16 23:59:59'
      AND `sub_status` = 'LB'
      THEN pause_sec
    END
  ) AS 'Long Break'
FROM
  `vicidial_agent_log`
GROUP BY `user`


我希望排除具有所有空值的条目,以下是我的语法,但它不起作用并产生相同的结果。

FROM
  `vicidial_agent_log`
WHERE 'Training Break' IS NOT NULL
  AND 'Short Break 1' IS NOT NULL
  AND 'Short Break 2' IS NOT NULL
  AND 'Long Break' IS NOT NULL
GROUP BY `user`

请在此提供帮助或分享一个可以提供帮助的 post。我一直在努力寻找但是

不要对列 names/aliases 使用单引号,因为例如 'Training Break' 在像 'Training Break' IS NOT NULL 这样的表达式中被解释为字符串文字,当然它不是空的。

在 MySql 中,您可以使用反引号将列名括起来。

此外,列 Training BreakShort Break 1Short Break 2Long Break 是聚合函数的结果,因此它们不能用于 WHERE 子句。而是使用 HAVING 子句。

最后,由于您想要这些列中至少有 1 列不为空的行,因此您应该使用运算符 OR 而不是 AND:

FROM
  `vicidial_agent_log`
GROUP BY `user`
HAVING `Training Break` IS NOT NULL
    OR `Short Break 1` IS NOT NULL
    OR `Short Break 2` IS NOT NULL
    OR `Long Break` IS NOT NULL

或:

FROM
  `vicidial_agent_log`
GROUP BY `user`
HAVING COALESCE(`Training Break`, `Short Break 1`, `Short Break 2`, `Long Break`) IS NOT NULL

首先,将 SUM() 表达式的公共部分移至 WHERE 子句。

其次,修复日期逻辑,这样你就不会错过一秒钟。

我也强烈、强烈推荐使用不需要转义的列名。所以:

SELECT user,
      SUM(CASE WHEN sub_status` = 'TB' THEN pause_sec END) AS Training_Break,
      SUM(CASE WHEN sub_status = 'SB1' THEN pause_sec END) AS Short_Break_1,
      SUM(CASE WHEN sub_status = 'SB2' THEN pause_sec END) AS Short_Break_2,
      SUM(CASE WHEN sub_status = 'LB' THEN pause_sec END) AS Long_Break
FROM vicidial_agent_log
WHERE event_time >= '2021-04-01' AND
      event_time < '2021-04-17'
GROUP BY user;

这可能会消除 NULL 值并加快查询速度。但是,如果整行中仍有 NULL 个值,那是因为有些行不具有这些状态之一。所以,只需检查它们:

SELECT user,
      SUM(CASE WHEN sub_status` = 'TB' THEN pause_sec END) AS Training_Break,
      SUM(CASE WHEN sub_status = 'SB1' THEN pause_sec END) AS Short_Break_1,
      SUM(CASE WHEN sub_status = 'SB2' THEN pause_sec END) AS Short_Break_2,
      SUM(CASE WHEN sub_status = 'LB' THEN pause_sec END) AS Long_Break
FROM vicidial_agent_log
WHERE event_time >= '2021-04-01' AND
      event_time < '2021-04-17' AND
      sub_status IN ('TB', 'SB1', 'SB2', 'LB')
GROUP BY user;

SQL 非常强大,可以让您在聚合之前或之后进行过滤。但是,通常(从性能的角度来看)在聚合之前进行过滤要好得多。

我还建议您使用不需要转义的标识符。并且,除了字符串和日期文字外,切勿对任何内容使用单引号。