Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 why is "group by" required (sql explanation needed

Author  Topic 

uberbloke
Yak Posting Veteran

67 Posts

Posted - 2001-07-02 : 06:31:31
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

   

- Advertisement -