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)
 Group By column names refusing to work

Author  Topic 

bobsie
Starting Member

2 Posts

Posted - 2008-02-19 : 11:42:57
Hi all

I'm having real problems here! I've read around plenty and just can't work this out. My Group By clause is being rejected if I include an either the column definition or the column name!

DECLARE @OnDay datetime
SET @OnDay = '20080131'

select datename(month, @OnDay) as 'MonthName', count(*) as 'Total'
from members
where isnull(substype, '') <> ''
and (leavedate is null or leavedate >= @OnDay)
and (joindate < @OnDay and isnull(joindate, '') <>'')
group by datename(month, @OnDay)


If I do it like the above the error is that I must use a column name in the group by clause, or if I do the following...

group by 'MonthName'


...it complains that the column name has to be in the select list - even though it clearly is!

Where am I going wrong??

Qualis
Posting Yak Master

145 Posts

Posted - 2008-02-19 : 11:58:31
You aren't aggregating on any column. What are you trying to accomplish? Can you give sample data from the members table and sample output you are looking for?
Go to Top of Page

bobsie
Starting Member

2 Posts

Posted - 2008-02-19 : 14:15:51
I didn't need to have a GROUP BY clause at all. (It had stayed in from a previous incarnation of a different query and all I had to do was take it out since I wasn't aggregating like that anyway in this query but just returning a month and a number.)

Sorry to bother you!
Go to Top of Page

Qualis
Posting Yak Master

145 Posts

Posted - 2008-02-19 : 15:27:21
No problem! We enjoy helping where we can.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-02-20 : 01:03:38
Also avoid having the alias names withing a single quote

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -