我有来自车辆,驾驶员和公司的带有“ UNION”的3个选择联接的查询。
select * from
(
(
select
department as "Delegación",
company as Compañía,
0 as "% Docs. Empresa",
count(distinct idClientTruck) as Vehículos,
round(sum(ko)/(docs*count(distinct idClientTruck))*100) as "% Docs. Vehículos",
0 as Conductores,
0 as "% Docs. Conductores"
from
(
select
cd.name as department,
c.businessname as company,
(select count(*) from widoc_config where idwidocconfiggroup=cfg_g.idwidocconfiggroup and idstatus=71001 and required=true) as docs,
wv.idClientTruck as idClientTruck,
cfg.idwidocconfig as idWidocConfig,
cfg.iddocument as idDocument,
a.idstatus as 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_vehicle wv on wv.idwidocclient=wc.idwidocclient and wv.idstatus=71001
join client_truck t on t.idclient_truck=wv.idclienttruck and t.idstatus_truckpart=33005
join widoc_fulfill_vehicle fv on fv.idwidocconfiggroup=cfg_g.idwidocconfiggroup and fv.idwidocvehicle=wv.idwidocvehicle
left join attachment_group a on a.idclient=wc.idclientAssociated and a.iddocument=cfg.iddocument and a.idstatus!=43004 and a.idFK=wv.idClientTruck
left join client_department cd on cd.idClientDepartment=wc.idClientDepartment and cd.idStatus=71001
where
cfg_g.idclient = 3683
#wc.idclientAssociated = 3683
and cfg_g.identitytype=73004
and cfg_g.idstatus=71001
)
v group by department,company
)
a UNION
(
select
department as "Delegación",
company as Compañía,
0 as "% Docs. Empresa",
0 as Vehículos,
0 as "% Docs. Vehículos",
count(distinct idDriver) as Conductores,
round(sum(ko)/(docs*count(distinct idDriver))*100) as "% Docs. Conductores"
from
(
select
cd.name as department,
c.businessname as company,
(select count(*) from widoc_config where idwidocconfiggroup=cfg_g.idwidocconfiggroup and idstatus=71001 and required=true) as docs,
wd.idDriver as idDriver,
cfg.idwidocconfig as idWidocConfig,
cfg.iddocument as idDocument,
a.idstatus as 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
)
x group by department,company
)
b UNION
(
select
department as "Delegación",
company as Compañía,
round(sum(ko)/docs*100) as "% Docs. Empresa",
0 as Vehículos,
0 as "% Docs. Vehículos",
0 as Conductores,
0 as "% Docs. Conductores"
from
(
select
cd.name as department,
c.businessname as company,
(select count(*) from widoc_config where idwidocconfiggroup=cfg_g.idwidocconfiggroup and idstatus=71001 and required=true) as docs,
cfg.idwidocconfig,
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_fulfill_client fc on fc.idwidocclient=wc.idwidocclient and fc.idwidocconfiggroup=cfg_g.idwidocconfiggroup
left join attachment_group a on a.idclient=c.idclient and a.iddocument=cfg.iddocument and a.idstatus!=43004
left join client_department cd on cd.idClientDepartment=wc.idClientDepartment and cd.idStatus=71001
# join attachment_doc d on d.idattachmentgroup=a.idattachmentgroup and d.idstatus!=43004
where
cfg_g.idclient = 3683
and cfg_g.identitytype=73002
and cfg_g.idstatus=71001
) t
group by department,company
)
c
);
发生的事情是,每次选择都会重复同一家公司,例如:
X公司-> 0个驾驶员0个文档。Empresa5辆车
X公司-> 4个驱动程序0个文档。Empresa0辆车
X公司-> 0位驾驶员9个文档。Empresa0辆车
我需要的是,当公司突然出现时,所有寄存器都位于同一行,我不知道自己是否在解释自己。
X公司-> 4位驾驶员9个文档。Empresa5辆车
编辑:我从该查询中进行了选择*,但“ b并”处的括号给了我这个错误:语法错误:缺少“右括号”。