I have the following query, I add that SUM() in "sum(count(distinct idDriver)) as Conductores", but i get this error:
Error Code: 1111. Invalid use of group function
select
department as Delegación,
company as Compañía,
config as Configuración,
docs as Documentos,
sum(count(distinct idDriver)) as Conductores,
sum(ko) as "Doc. incorrectos",
round(sum(ko)/(docs*count(distinct idDriver))*100) as "% incumplimiento"
from
(
select
cd.name as department,
cfg_g.name as config,
c.businessname as company,
(select count(*) from widoc_config where idwidocconfiggroup=cfg_g.idwidocconfiggroup and idstatus=71001 and required=true) as docs,
wd.idDriver,
cfg_g.idwidocconfiggroup,
cfg.idwidocconfig,
cfg.iddocument,
a.idstatus,
if(a.idstatus=43002,"0","1") as ko
from
widoc_config_group cfg_g
join widoc_config cfg on cfg.idwidocconfiggroup=cfg_g.idwidocconfiggroup and cfg.idstatus=71001 and cfg.required=true
join widoc_client wc on wc.idclient=cfg_g.idclient and wc.idstatus=71001
join client c on c.idclient=wc.idclientassociated and c.idstatus_client=5004
join widoc_driver wd on wd.idwidocclient=wc.idwidocclient and wd.idstatus=71001
join driver d on d.iddriver=wd.iddriver and d.idstatus=71001
join widoc_fulfill_driver fd on fd.idwidocconfiggroup=cfg_g.idwidocconfiggroup and fd.idwidocdriver=wd.idwidocdriver
left join attachment_group a on a.idclient=wc.idclientAssociated and a.iddocument=cfg.iddocument and a.idstatus!=43004 and a.idFK=wd.idDriver
left join client_department cd on cd.idClientDepartment=wc.idClientDepartment and cd.idStatus=71001
where
cfg_g.idclient = 3683
#wc.idclientAssociated = 1865
and cfg_g.identitytype=73003
and cfg_g.idstatus=71001
) t
group by department,company;
Where do I have to put this HAVING code, and what should I have to write? Thanks!
Here:
sum(count(distinct idDriver)) as Conductores,
sum(ko) as "Doc. incorrectos",
round(sum(ko)/(docs*count(distinct idDriver))*100) as "% incumplimiento"
You cannot nest aggregate functions: if you think about it, sum(count(...))
makes no sense. I suspect that you want:
count(distinct idDriver) as Conductores,
sum(ko) as "Doc. incorrectos",
round(sum(ko)/(docs*count(distinct idDriver))*100) as "% incumplimiento"
Also, you should enumerate all non-aggregate columns in the group by
clause. Even if currently missing columns are functionnaly dependant on the ones being listed, this is still a best practice in SQL (and a rule on most RDBMS, including MySQL unless option FULL_GROUP_BY_ONLY
is disabled). So you want to change this:
group by department,company
To:
group by department, company, config, docs
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.