Home > Software engineering >  Why the join is not working without group by statement
Why the join is not working without group by statement

Time:08-26

If I remove the group by statement then it is showing an error.

SELECT COMPANY.COMPANY_CODE,
company.founder,
count( distinct lm.lead_manager_code),
count(distinct  sm.senior_manager_code),
count(distinct  m.manager_code),
count(distinct  e.employee_code)
from Company 
join Lead_Manager as lm
on Company.company_code=lm.company_code
join Senior_Manager as sm on Company.company_code=sm.company_code
join Manager as m on Company.company_code=m.company_code
join Employee as e on Company.company_code=e.company_code
GROUP BY Company.COMPANY_CODE, Company.FOUNDER
ORDER BY Company.COMPANY_CODE ASC
;

CodePudding user response:

Basically, you are using an aggregate function so you need to group

select count(amount) from Orders group by customer_id

Orders order_id item amount customer_id 1 Keyboard 400 4 2 Mouse 300 4 3 Monitor 12000 3 4 Keyboard 400 1 5 Mousepad 250 2

Now using group by on customer_id so we have id 4 so need to check the count of 4 id with group by

  • Related