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 2005 Forums
 Transact-SQL (2005)
 group by with condition

Author  Topic 

hai
Yak Posting Veteran

84 Posts

Posted - 2007-10-15 : 15:35:27
table structure:
id,fd1,fd2,fd3,fd4,ti:

I need to
select id, fd1,fd2,fd3,fd4,max(ti) from table
group by fd1,fd2,fd3,fd4.

I get error complaining id is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY

any help on this is greatly appreicate...

thanks

windows 2003 sql 2005

Kristen
Test

22859 Posts

Posted - 2007-10-15 : 15:46:23
You need to EITHER include [id] in the GROUP BY clause, or apply an aggregate function to it - like MAX(ID)

Kristen
Go to Top of Page

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2007-10-16 : 11:30:23
One strategy you could apply to get around this problem is to break the sql statement down into 2 parts, do an initial CTE , for the preliminary work and then do the second statment

Jack Vamvas
--------------------
Search IT jobs from multiple sources- http://www.ITjobfeed.com
Go to Top of Page
   

- Advertisement -