我有一个执行 2 个 LEFT JOIN 和 1 个 GROUP BY 的查询,但缺少结果:
以下查询应返回大约 1159 个结果,但只返回大约 880 个结果; 如果我只加入“c”宽度“mm”,我仍然会得到 1159 个结果。只有当所有 3 个表都得到 880 个结果时。我也使用了但没有运气:RIGHT JOIN、RIGHT OUTER JOIN、LEFT OUTER JOIN、INNER JOIN。我尝试在 JOIN 语法中使用“()”和不使用“()”,但也没有运气。
任何帮助表示赞赏
SELECT c.ccod, c.cetcm, c.cdscr, c.cpcul, c.cdent, mm.amexi,
Sum(CASE WHEN m.mcdmv=11 THEN m.mqtd END),
Sum(CASE WHEN m.mcdmv=2 THEN m.mqtd END),
Sum(CASE WHEN m.mcdmv=13 THEN m.mqtd END),
Sum(CASE WHEN m.mcdmv=14 THEN m.mqtd END),
MAX(CASE WHEN m.mcdmv=82 THEN m.mdata END)
FROM
(zzz.ccc c
LEFT JOIN zzz.mmmm mm ON c.crnp = mm.arncd)
LEFT JOIN zzz.mm m ON m.mrncd = c.crnp
WHERE
((c.cetcm='xcd') OR
(c.cetcm='ewfwe') OR
(c.cetcm='fewfew') OR
(c.cetcm='fewf')) AND
(m.MDATA Between 20100101 And 20110406) AND
((m.mcdmv=11) OR
(m.mcdmv=12) OR
(m.mcdmv=13) OR
(m.mcdmv=14)) AND
(mm.aarm=1)
GROUP BY c.ccod, c.cetcm, c.cdscr, c.cpcul, c.cdent, mm.amexi
ORDER BY c.ccod
The following query should return around 1159 results but only returns around 880 results;
If I only JOIN the "c" width "mm" I still get the 1159 results. Only when all 3 tables I get 880 results. I also used with no luck: RIGHT JOIN, RIGHT OUTER JOIN, LEFT OUTER JOIN, INNER JOIN. I tried with "()" and without "()" in the JOIN sintax no luck too.
Any help is appreciated
SELECT c.ccod, c.cetcm, c.cdscr, c.cpcul, c.cdent, mm.amexi,
Sum(CASE WHEN m.mcdmv=11 THEN m.mqtd END),
Sum(CASE WHEN m.mcdmv=2 THEN m.mqtd END),
Sum(CASE WHEN m.mcdmv=13 THEN m.mqtd END),
Sum(CASE WHEN m.mcdmv=14 THEN m.mqtd END),
MAX(CASE WHEN m.mcdmv=82 THEN m.mdata END)
FROM
(zzz.ccc c
LEFT JOIN zzz.mmmm mm ON c.crnp = mm.arncd)
LEFT JOIN zzz.mm m ON m.mrncd = c.crnp
WHERE
((c.cetcm='xcd') OR
(c.cetcm='ewfwe') OR
(c.cetcm='fewfew') OR
(c.cetcm='fewf')) AND
(m.MDATA Between 20100101 And 20110406) AND
((m.mcdmv=11) OR
(m.mcdmv=12) OR
(m.mcdmv=13) OR
(m.mcdmv=14)) AND
(mm.aarm=1)
GROUP BY c.ccod, c.cetcm, c.cdscr, c.cpcul, c.cdent, mm.amexi
ORDER BY c.ccod
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如所建议的,您需要将左联接中针对表的条件移至各自的 ON 子句中:
此外,我还使用了 IN 函数,它可以让您传递一系列值。顺便说一句,Access 也可能会因为没有列聚合函数的列别名而犹豫不决。在这里我只是使用了一些简单的东西。
As has been suggested, you need to move the criteria against the tables in the Left Joins into their respective ON clauses:
In addition, I've made use of the IN function which lets you pass a series of values. Btw, Access will likely also balk at not having column aliases for the columns aggregate functions. Here I've just used something simple.