我想知道如何按同时具有 select count()
和 count()
语句的字段进行分组。我知道我们必须将所有选择字段放入 group by 中,但由于字段中的第二个 count()
语句,它不会让我这样做。
create table C as(
select a.id, a.date_id,
(select count(b.hits)*1.00 where b.hits >= '9')/count(b.hits) AS percent **<--error here
from A a join B b
on a.id = b.id
group by 1,2,3) with no data primary index(id);
这是我的错误:
[SQLState HY000] GROUP BY and WITH...BY clauses may not contain
aggregate functions. Error Code: 3625
当我将选择添加到第三行的第二个计数时,只能得到 1 或 0,这是不正确的。
`((select count(b.hits)*1.00 where b.hits >= '9')/(select count(b.hits))) AS` percent
我是否需要进行自连接,或者有什么方法可以只使用嵌套查询?
最佳答案
您需要修复分组依据
。但是,您可以将查询简化为:
create table C as
select a.id, a.date_id,
avg(b.hits >= 9) as percent
from A a join
B b
on a.id = b.id
group by a.id, a.date_id
with no data primary index(id);
关于mysql - 如何按同时具有选择和计数的字段进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31281568/