过滤不同的计数

发布于 2025-01-10 09:40:50 字数 579 浏览 0 评论 0原文

我想计算每个类别的羽毛在数据集中出现的次数,然后当 beak 列的类别为 long< 时,仅筛选那些已计数超过 5 次的羽毛/代码>。

但是,我收到以下错误:

“(”附近:语法错误

SELECT 
    land_birds.feather, land_birds.weight, COUNT(DISTINCT land_birds.feather) AS numFeathers,
    land_birds.size, sea_birds.beak
     
FROM
    land_birds
INNER JOIN
    sea_birds
ON 
    land_birds.colour = sea_birds.colour
WHERE sea_birds.colour IN (SELECT colour from land_birds) AND beak LIKE 'Long'
GROUP BY feather
ORDER BY feather ASC
FILTER(WHERE numFeathers > 5)

I want to count the number of times each category of feathers appear in the dataset, and then filter only for those that have been counted more than 5 times when the column beak has the category long.

However, I'm getting the following error:

near "(": syntax error

SELECT 
    land_birds.feather, land_birds.weight, COUNT(DISTINCT land_birds.feather) AS numFeathers,
    land_birds.size, sea_birds.beak
     
FROM
    land_birds
INNER JOIN
    sea_birds
ON 
    land_birds.colour = sea_birds.colour
WHERE sea_birds.colour IN (SELECT colour from land_birds) AND beak LIKE 'Long'
GROUP BY feather
ORDER BY feather ASC
FILTER(WHERE numFeathers > 5)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

泪是无色的血 2025-01-17 09:40:50

要过滤通过分组生成的信息,您可以使用紧随 GROUP BY 子句放置的 HAVING 子句,如下所示:

SELECT
      land_birds.feather
    , land_birds.weight
    , COUNT(DISTINCT land_birds.feather) AS numFeathers
    , land_birds.size
    , sea_birds.beak
FROM land_birds
INNER JOIN sea_birds ON land_birds.colour = sea_birds.colour
WHERE beak LIKE 'Long'
GROUP BY land_birds.feather
HAVING COUNT(DISTINCT land_birds.feather) > 5
ORDER BY land_birds.feather ASC 

虽然在having 子句中看起来合乎逻辑要使用您提供的计算中的“numFeathers”别名,不要。请参考计算本身。记住这一点可能会有所帮助,您可以在having子句中引用未出现在select子句中的分组计算,例如,这仍然有效

SELECT
      land_birds.feather
    , land_birds.weight

    , land_birds.size
    , sea_birds.beak
FROM land_birds
INNER JOIN sea_birds ON land_birds.colour = sea_birds.colour
WHERE beak LIKE 'Long'
GROUP BY land_birds.feather
HAVING COUNT(DISTINCT land_birds.feather) > 5
ORDER BY land_birds.feather ASC 

,这里该计算根本没有列别名。


关于您的查询的其他观察结果。

  1. 在整个查询中引用列时,始终使用表名称(或表别名)。
  2. 有关颜色的内连接条件意味着结果中只能有与该条件完全匹配的行。因此,不需要在 where 子句中也包含相同的条件。

最后一点,请不要将having 子句视为where 子句的替代品。 where 子句出现在分组之前,因此减少了要分组的数据量。 having 子句过滤生成的信息,这些信息只能在分组后存在。简而言之,它们是非常不同的条款,具有特定的功能和用途。

To filter on information that is generated by grouping you use a HAVING clause which is placed immediately after the GROUP BY clause, like this:

SELECT
      land_birds.feather
    , land_birds.weight
    , COUNT(DISTINCT land_birds.feather) AS numFeathers
    , land_birds.size
    , sea_birds.beak
FROM land_birds
INNER JOIN sea_birds ON land_birds.colour = sea_birds.colour
WHERE beak LIKE 'Long'
GROUP BY land_birds.feather
HAVING COUNT(DISTINCT land_birds.feather) > 5
ORDER BY land_birds.feather ASC 

Whilst it may seem logical in the having clause to use the "numFeathers" alias you gave that calculation, don't. Reference the calculation itself instead. It may help to remember this that you can reference a grouping calculation in the having clause that does not appear in the select clause e.g. this would still work

SELECT
      land_birds.feather
    , land_birds.weight

    , land_birds.size
    , sea_birds.beak
FROM land_birds
INNER JOIN sea_birds ON land_birds.colour = sea_birds.colour
WHERE beak LIKE 'Long'
GROUP BY land_birds.feather
HAVING COUNT(DISTINCT land_birds.feather) > 5
ORDER BY land_birds.feather ASC 

Here that there is no column alias at all for that calculation.


Other observations about your query.

  1. always use the table name (or table alias) when referring to columns throughout your query
  2. the inner join condition about colour means there can only be rows in the result that exactly match that condition. Hence it is NOT required that you also include the same condition in the where clause.

One final note, please don't treat the having clause as a substitute for a where clause. The where clause occurs before the grouping and hence it reduces the amount of data to be grouped. A having clause filters on the generated information which can only exist after grouping. In short they are very different clauses with specific capabilities and uses.

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