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
 Transact-SQL (2000)
 sql invalid in select error due to group by clause

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-02-27 : 12:07:11
Megha writes "When I run a query like


select distinct b.h_userid as h_userid,h_fname,h_lname,a.h_orderid as h_orderid,h_date,sum((c.h_prdprice * c.h_qty) + c.h_tax) as total from hs_orders a,hs_users b ,hs_orderdetails c where h_fname='demo' and a.h_userid = b.h_userid and a.h_orderid = c.h_orderid


I get these errors...


Server: Msg 8118, Level 16, State 1, Line 1
Column 'b.H_Userid' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'b.H_Fname' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'b.H_Lname' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'a.H_orderid' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'a.H_Date' is invalid

I can solve them by putting the column names in group by and order by clause in the same query . But I have come accross this many times and I do not know why sql forces me to use group by and order by when I dont want to...
Theres some inner mechanics that I am not aware of...Can you please tell me more about it..

Thanks a lot
Regards,
Megha"

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2006-02-27 : 12:25:41
You are using an aggregate function and GROUP BY will group all the other fields which have the same data together to make your sum work, otherwise SUM is a useless function.
Go to Top of Page
   

- Advertisement -