过滤不同的计数
我想计算每个类别的羽毛在数据集中出现的次数,然后当 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要过滤通过分组生成的信息,您可以使用紧随
GROUP BY
子句放置的HAVING
子句,如下所示:虽然在having 子句中看起来合乎逻辑要使用您提供的计算中的“numFeathers”别名,不要。请参考计算本身。记住这一点可能会有所帮助,您可以在having子句中引用未出现在select子句中的分组计算,例如,这仍然有效
,这里该计算根本没有列别名。
关于您的查询的其他观察结果。
最后一点,请不要将having 子句视为where 子句的替代品。 where 子句出现在分组之前,因此减少了要分组的数据量。 having 子句过滤生成的信息,这些信息只能在分组后存在。简而言之,它们是非常不同的条款,具有特定的功能和用途。
To filter on information that is generated by grouping you use a
HAVING
clause which is placed immediately after theGROUP BY
clause, like this: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
Here that there is no column alias at all for that calculation.
Other observations about your query.
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.