SQL 查询给出错误的总和
我正在使用 Excel 附带的相当旧的 Microsoft Query 来查询 ODBC 数据库。然而,当我连接两个表时,它给了我错误的总和。
这工作正常:
SELECT accountcode, SUM(tr_amount)
FROM deb_trans deb_trans
WHERE (today() > dr_tr_due_date + 14)
GROUP BY accountcode
但是,这不行:
SELECT deb_trans.accountcode, Sum(deb_trans.tr_amount)
FROM deb_trans deb_trans, mailer_master mailer_master
WHERE (today()>dr_tr_due_date+14) AND (mailer_master.accountcode=deb_trans.accountcode)
GROUP BY deb_trans.accountcode
加入的字段是 accountcode。
字段 tr_amount 源自 deb_trans 表。它不存在于 mailer_master 中。
有什么想法吗?谢谢你们!
I'm using the rather old Microsoft Query that comes with Excel to query an ODBC database. However it's giving me the wrong sum when I join two tables.
This works fine:
SELECT accountcode, SUM(tr_amount)
FROM deb_trans deb_trans
WHERE (today() > dr_tr_due_date + 14)
GROUP BY accountcode
However, this does not:
SELECT deb_trans.accountcode, Sum(deb_trans.tr_amount)
FROM deb_trans deb_trans, mailer_master mailer_master
WHERE (today()>dr_tr_due_date+14) AND (mailer_master.accountcode=deb_trans.accountcode)
GROUP BY deb_trans.accountcode
The joined field being accountcode.
The field tr_amount orginates from the deb_trans table. It is not present in mailer_master.
Any ideas? Thanks guys!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果连接表,您会在分组之前为每个组合获取与过滤条件相对应的行。在本例中:按日期过滤的每个 deb_trans 和 mailer_master 组合的一行。如果您想要一个有效的总和,则不应以更改行数(分组之前)的方式连接另一个表。
If you join the tables, you get a row for each combination which corresponds to the filter criteria before it is grouped. In this case: a row for each deb_trans and mailer_master combination filtered by date. If you want a valid sum, you should not join another table the way that the number of rows (before grouping) is changed.