Dear Mr.Barand,
Thanks for your response for my post.
I am writing the query to get the output of actual and target of three category from different tables.
When I am writing query separately each category I am getting output. But when i am joining those queries, I am getting errors.
For example,
the below query to get the target and actual of machine details.
SELECT Month,Name, ProductionTotal, ProductionActual FROM
( select count(a.id) as ProductionActual, Month, Fname from sbms.production as a
inner join sbms.usertable as b on b.EmpNo = a.EmpID
where a.Branch = '5' AND (b.Role='4' OR b.Role='5') AND NOT (MCubicmeter = '0' AND MHourmeter ='0') AND Month='2024-01' group by Month, EmpID) ProductionActual
LEFT JOIN (select count(a.id) as ProductionTotal, Month, Fname as Name from sbms.production as a
inner join sbms.usertable as b on b.EmpNo = a.EmpID
where a.Branch = '5' AND (b.Role='4' OR b.Role='5') AND Month='2024-01' group by Month, EmpID
) ProductionTotal USING (Month)
I am getting the below output.
Next when i am writing the below query to get the output if customer target and actual.
SELECT Month,Name,CustomerTotal,CustomerActual FROM
( select count(a.id) as CustomerActual, Month, Fname as Name from sbms.customerdata as a
inner join sbms.usertable as b on b.EmpNo = a.EmpID
where a.Branch = '5' AND (b.Role='4' OR b.Role='5') AND (VisitType = 'No Due' OR VisitDate !='') AND Month='2024-04'
group by Month, EmpID) CustomerActual
LEFT JOIN (select count(a.id) as CustomerTotal, Month, Fname as Name from sbms.customerdata as a
inner join sbms.usertable as b on b.EmpNo = a.EmpID
where a.Branch = '5' AND (b.Role='4' OR b.Role='5') AND Month='2024-04' group by Month, EmpID
) CustomerTotal USING (Month, Name)
I am getting the below output.
When i am joining the above two query i am getting the category output only production output is not coming. Which ever is first, that output only coming.
the join query is.
SELECT Month,Name,CustomerTotal,CustomerActual, ProductionTotal, ProductionActual FROM
( select count(a.id) as CustomerActual, Month, Fname as Name from sbms.customerdata as a
inner join sbms.usertable as b on b.EmpNo = a.EmpID
where a.Branch = '5' AND (b.Role='4' OR b.Role='5') AND (VisitType = 'No Due' OR VisitDate !='') AND Month='2024-04'
group by Month, EmpID) CustomerActual
LEFT JOIN (select count(a.id) as CustomerTotal, Month, Fname as Name from sbms.customerdata as a
inner join sbms.usertable as b on b.EmpNo = a.EmpID
where a.Branch = '5' AND (b.Role='4' OR b.Role='5') AND Month='2024-04' group by Month, EmpID
) CustomerTotal USING (Month, Name)
LEFT JOIN ( select count(a.id) as ProductionActual, Month, Fname as Name from sbms.production as a
inner join sbms.usertable as b on b.EmpNo = a.EmpID
where a.Branch = '5' AND (b.Role='4' OR b.Role='5') AND NOT (MCubicmeter = '0' AND MHourmeter ='0')
AND Month='2024-01' group by Month, EmpID) ProdAct USING (Month, Name)
LEFT JOIN (select count(a.id) as ProductionTotal, Month, Fname as Name from sbms.production as a
inner join sbms.usertable as b on b.EmpNo = a.EmpID
where a.Branch = '5' AND (b.Role='4' OR b.Role='5') AND Month='2024-01' group by Month, EmpID
) ProductionTotal USING (Month, Name)
and the join query output is
single table is using to get the target and actual using above query
But i am using two tables to get the target and actual for the third category marketing.
Marketing query is
SELECT Month,Fname,MarketingActual,MarketingTotal FROM
( select count(a.id) as MarketingActual, Month, Fname from sbms.marketing_data as a
inner join sbms.usertable as b on b.EmpNo = a.EmpID
where b.Branch = '5' AND (b.Role='4' OR b.Role='5') AND Month='2024-01' group by Month, EmpID) MarketingActual
LEFT JOIN (select count(a.id) as MarketingTotal, Month, Fname as Name from sbms.marketing_target as a
inner join sbms.usertable as b on b.EmpNo = a.EmpID
where b.Branch = '5' AND (b.Role='4' OR b.Role='5') AND Month='2024-01' group by Month, EmpID) MarketingTotal USING (Month)
and the output is
But the marketing output should come like below image
I need to combine all these three categories and the final output should come like below.
Al my dumps are attached with the blow link for your reference.
https://drive.google.com/file/d/1pRranTejVygkSaufLxjIbiiFdfyxlSDq/view?usp=sharing
Please help me to solve this.