将 NULL 替换为 Mysql 中 WHERE 子句中的 Date

Replace NULL with the Date in the WHERE clause in Mysql

所以,为了在我的计数列中得到 0,我尝试了这个有效的查询。

SELECT b.TXT_RECORD_DATE, a.TXT_CALL_TYPE, 
      SUM(CASE 
        WHEN b.TXT_CALL_TYPE IS NOT NULL
          THEN 1
        ELSE 0
      END) AS StatusCount
FROM (
  SELECT DISTINCT TXT_CALL_TYPE
  FROM CDR
  ) a
LEFT JOIN CDR b ON a.TXT_CALL_TYPE = b.TXT_CALL_TYPE AND b.TXT_RECORD_DATE IN ('2022-04-12', '2022-04-13','2022-04-14')
GROUP BY a.TXT_CALL_TYPE, b.TXT_RECORD_DATE;

但它在 StatusCount = 0

的列中显示 [NULL]

所以我的问题是有没有办法实际分配当前正在搜索的日期而不是 [NULL]

当前结果是这样的

TXT_RECORD_DATE TXT_CALL_TYPE StatusCount
BRD 0
2022-04-12 Busy Call 9
IDIN 0
IDOT 0
2022-04-12 Incoming - Missed Call 133
2022-04-13 Incoming - Missed Call 38
2022-04-14 Incoming - Missed Call 29
ITRS 0
IVIN 0
2022-04-12 IVOT 21
2022-04-13 IVOT 27
2022-04-14 IVOT 20
PIN 0
2022-04-12 POT 1
2022-04-12 PTRS 19
2022-04-13 PTRS 4
2022-04-14 PTRS 14

抱歉,如果我忘记了什么或不清楚。我在半夜给你写信,实在是太累了。无论如何谢谢。你们总是很棒。

您需要生成一个您感兴趣的日期列表,然后 CROSS JOIN 生成一个呼叫类型列表;然后你可以 LEFT JOIN 到通话记录以获得你想要的结果。在 MariaDB 中,您可以使用 sequence storage engine 轻松生成日期列表:

SELECT d.TXT_RECORD_DATE, a.TXT_CALL_TYPE, 
      SUM(CASE 
        WHEN b.TXT_CALL_TYPE IS NOT NULL
          THEN 1
        ELSE 0
      END) AS StatusCount
FROM (
  SELECT '2022-04-12' + INTERVAL (seq) DAY AS TXT_RECORD_DATE
  FROM seq_0_to_2
) d
CROSS JOIN (
  SELECT DISTINCT TXT_CALL_TYPE
  FROM CDR
  WHERE TXT_CALL_TYPE IS NOT NULL
) a
LEFT JOIN CDR b ON a.TXT_CALL_TYPE = b.TXT_CALL_TYPE AND d.TXT_RECORD_DATE = b.TXT_RECORD_DATE
GROUP BY d.TXT_RECORD_DATE, a.TXT_CALL_TYPE

Demo on dbfiddle