Can someone please explain this for me as I am confused as to what GROUP BY is doing.I have the following query in a stored procedure, this works fine select distinct CO.company_name from COMPANYS CO inner join USERS U on U.uidcompany = CO.uidcompany inner join SESSIONS S on S.uiduser = U.uiduser inner join ORDERS O on O.uidsession = S.uidsession where uidmasterorder = 0 and uidstatus = 3
when I add ,count(O.uidorder) as NumberOfOrders
to the select clause I get an error message, column 'co.company_name' is invalid in the select list because it is not containted in an aggregate function and there is no GROUP BY clause
If I then add the GROUP BY clause in the query works.Can someone explain what is going on, I am quite happy to keep adding clauses to GROUP BY but would like to understand why