对这些查询中无意义的表/列名称先发制人地道歉。如果您曾经使用过 Remedy 的数据库后端,您就会明白。
当我怀疑实际值应该在 20 的某个地方(我相信是 23)时,我遇到了 Count Distinct 返回空值的问题。下面是一系列查询及其返回值。
SELECT count(distinct t442.c1)
FROM t442, t658, t631
WHERE t442.c1 = t658.c536870930
AND t442.c200000003 = 'Network'
AND t442.c536871139 < 2
AND t631.c536870913 = t442.c1
AND t658.c536870925 = 1
AND (t442.c7 = 6 OR t442.c7 = 5)
AND t442.c536870954 > 1141300800
AND (t442.c240000010 = 0)
结果 = 497。
添加表 t649 并确保它具有链接回表 t442 的记录:
SELECT COUNT (DISTINCT t442.c1)
FROM t442, t658, t631, t649
WHERE t442.c1 = t658.c536870930
AND t442.c200000003 = 'Network'
AND t442.c536871139 < 2
AND t631.c536870913 = t442.c1
AND t658.c536870925 = 1
AND (t442.c7 = 6 OR t442.c7 = 5)
AND t442.c536870954 > 1141300800
AND (t442.c240000010 = 0)
AND t442.c1 = t649.c536870914
结果 = 263。
过滤掉表 t649 中列 c536870939 <= 1 的记录:
SELECT COUNT (DISTINCT t442.c1)
FROM t442, t658, t631, t649
WHERE t442.c1 = t658.c536870930
AND t442.c200000003 = 'Network'
AND t442.c536871139 < 2
AND t631.c536870913 = t442.c1
AND t658.c536870925 = 1
AND (t442.c7 = 6 OR t442.c7 = 5)
AND t442.c536870954 > 1141300800
AND (t442.c240000010 = 0)
AND t442.c1 = t649.c536870914
AND t649.c536870939 > 1
结果 = 24。
过滤 HAVING 语句:
SELECT COUNT (DISTINCT t442.c1)
FROM t442, t658, t631, t649
WHERE t442.c1 = t658.c536870930
AND t442.c200000003 = 'Network'
AND t442.c536871139 < 2
AND t631.c536870913 = t442.c1
AND t658.c536870925 = 1
AND (t442.c7 = 6 OR t442.c7 = 5)
AND t442.c536870954 > 1141300800
AND (t442.c240000010 = 0)
AND t442.c1 = t649.c536870914
AND t649.c536870939 > 1
HAVING COUNT (DISTINCT t631.c536870922) =
COUNT (DISTINCT t649.c536870931)
结果 = 空。
如果我运行以下查询,我在结果列表中看不到任何可以解释为什么我没有得到任何类型的返回值的内容。即使我从 SELECT 中删除了 DISTINCT,也是如此。(我分别得到 25 和 4265 行数据)。
SELECT DISTINCT t442.c1, t631.c536870922, t649.c536870931
FROM t442, t658, t631, t649
WHERE t442.c1 = t658.c536870930
AND t442.c200000003 = 'Network'
AND t442.c536871139 < 2
AND t631.c536870913 = t442.c1
AND t658.c536870925 = 1
AND (t442.c7 = 6 OR t442.c7 = 5)
AND t442.c536870954 > 1141300800
AND (t442.c240000010 = 0)
AND t442.c1 = t649.c536870914
AND t649.c536870939 > 1
我还有其他几个地方的查询设置与返回空值的查询完全一样,并且它工作得非常好——返回正确值的可用数字。我必须假设在这种情况下任何独特之处都与数据有关,而不是与实际查询有关,但我不确定在数据中寻找什么来解释它。在聚合之前,我无法在原始数据中找到任何空值。我不知道还有什么会导致这种情况。
任何帮助,将不胜感激。