SQL 计数不同

发布于 2024-11-14 14:55:44 字数 893 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫七 2024-11-21 14:55:44

您的 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.

深居我梦 2024-11-21 14:55:44

试试这样:

SELECT CountyName, CountyID,
(
    SELECT COUNT(DISTINCT(STFO.ShippingZip))
    FROM Orders STFO, SwedishZipCodes07
    WHERE STFO.intShippingCountryID = 202
    AND STFO.ShippingZip > 0
    AND STFO.ShippingZip = SwedishZipCodes07.ZipCode
    AND SwedishZipCodes07.CountyId = ZipWrapper.CountyID

) AS Count
FROM Zipcodes ZipWrapper
ORDER BY ZipWrapper.CountyID

Try it this way:

SELECT CountyName, CountyID,
(
    SELECT COUNT(DISTINCT(STFO.ShippingZip))
    FROM Orders STFO, SwedishZipCodes07
    WHERE STFO.intShippingCountryID = 202
    AND STFO.ShippingZip > 0
    AND STFO.ShippingZip = SwedishZipCodes07.ZipCode
    AND SwedishZipCodes07.CountyId = ZipWrapper.CountyID

) AS Count
FROM Zipcodes ZipWrapper
ORDER BY ZipWrapper.CountyID
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文