SQL 计数不同
我有一个 SQL 查询,它只计算并返回正确的值,但是当我将其分割并获取不同区域的计数时,总计数不再与第一个匹配。
这就是我得到的:
表邮政编码,其中包含 CountyID、CountyName 和邮政编码。
包含 ShippingCountry & 的表订单ShippingZip
第一个计数如下所示:
SELECT COUNT(STFO.ShippingZip)
FROM Orders
WHERE
AND STFO.intShippingCountryID = 202
AND STFO.ShippingZip IN (SELECT DISTINCT ZipCode FROM SwedishZipCodes07)
AND STFO.ShippingZip > 0
在一个示例中,我得到的计数为 3007,这是正确的。 在我的下一个计数中,它看起来更像是这样:
SELECT DISTINCT CountyName, CountyID,
(SELECT COUNT(STFO.ShippingZip)
FROM Orders
WHERE
AND STFO.intShippingCountryID = 202
AND STFO.ShippingZip IN (SELECT DISTINCT ZipCode FROM SwedishZipCodes07 WHERE CountyID = ZipWrapper.CountyID)
AND STFO.ShippingZip > 0) AS Count
FROM Zipcodes ZipWrapper
ORDER BY ZipWrapper.CountyID
在同一个示例中,我现在得到的计数类似于 3018。 (在我的示例中,查询被更具体地过滤,但它们都相互匹配,差异在于我的简化示例代码中)。
I have a SQL query that just count and it returns the correct value, but when i slice it up and get the count for the different areas the total count no longer match the first.
This is what i got:
Table zipcodes, that contain a CountyID, CountyName & Zipcode.
Table Orders that contains ShippingCountry & ShippingZip
The first count looks like this:
SELECT COUNT(STFO.ShippingZip)
FROM Orders
WHERE
AND STFO.intShippingCountryID = 202
AND STFO.ShippingZip IN (SELECT DISTINCT ZipCode FROM SwedishZipCodes07)
AND STFO.ShippingZip > 0
In one example i get this count to 3007 wich is correct.
In my next count it looks more like this:
SELECT DISTINCT CountyName, CountyID,
(SELECT COUNT(STFO.ShippingZip)
FROM Orders
WHERE
AND STFO.intShippingCountryID = 202
AND STFO.ShippingZip IN (SELECT DISTINCT ZipCode FROM SwedishZipCodes07 WHERE CountyID = ZipWrapper.CountyID)
AND STFO.ShippingZip > 0) AS Count
FROM Zipcodes ZipWrapper
ORDER BY ZipWrapper.CountyID
In the very same example i now get count like 3018.
(In my examples the query is filtered more specific but they both match each other, the differences are in my simplified example code here).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 2 个不同的 CountyID 可能具有相同的 STFO.ShippingZip。
这就是为什么在第二个查询中它将添加到每个 CountyID 而在第一个查询中仅添加一次。
You may have the same STFO.ShippingZip for 2 different CountyIDs.
That's why in the second query it will add to each CountyID and in the first one it is added only once.
试试这样:
Try it this way: