sql查询的百分比,无效使用组函数

时间:2017-12-20 14:45:12

标签: mysql sql

出于某种原因,我遇到了使此查询生效的问题。

我正在尝试在第3行获得一个百分比,其中我将curdate()小于d.next_call_date + 7 days的记录数除以总联系人,即count(*)然后分组用户。我已经完成了这个记录计数,但是当我尝试这个百分比时,我一直收到错误1111无效使用组功能。

如果我删除第3行它工作正常,所以我知道错误就在那里。

对某人来说,我做错了吗?

select 
 c.user as UserID,
 sum(curdate() < (d.next_call_date + interval 7 day ) / count(*)) as upToDate
From jfi_dealers.contact_events c
join Jackson_id.users u
on c.user = u.id
join jfi_dealers.dealers d
on c.dealer_num = d.dealer_num
where c.user = u.id
group by c.user;

2 个答案:

答案 0 :(得分:1)

括号位于错误的位置,因此count(*)嵌套在sum()中。但是你应该能够使用avg()函数完全消除它:

select c.user as UserID,
       avg(curdate() < (d.next_call_date + interval 7 day ) ) as upToDate
. . .

答案 1 :(得分:0)

count(*)移出sum()

select 
 c.user as UserID,
 sum(curdate() < (d.next_call_date + interval 7 day )) / count(*) as upToDate
From jfi_dealers.contact_events c
join Jackson_id.users u
on c.user = u.id
join jfi_dealers.dealers d
on c.dealer_num = d.dealer_num
group by c.user;

我还删除了where子句,因为它只包含重复的连接条件。