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)
 Error with Case When, Group By sql elements

Author  Topic 

netsports
Starting Member

11 Posts

Posted - 2005-09-01 : 18:48:46
I'm trying to create an alias called neworders for a field in an sql statement, but when I include both the aliased statement in the select portion, and mirror it in the Group By clause:

neworders = CASE WHEN orders.Sale_Type_Id <3 AND accounts.onBoard = 1 THEN orders.Sale_type_ID ELSE NULL END ,

..i get an error "invalid syntax at '=' ", which means the CASE WHEN thing doesnt work with Group By clauses. What is a better way , can I do a select sub-query to create my 'neworders' alias?

SamC
White Water Yakist

3467 Posts

Posted - 2005-09-01 : 19:14:00
It would help to see the complete query, including the GROUP BY
Go to Top of Page

nosepicker
Constraint Violating Yak Guru

366 Posts

Posted - 2005-09-01 : 19:46:59
I hope you're not including the "neworders = " part in the GROUP BY clause. If so, remove that part.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-09-02 : 02:10:39
Whenever posting query related question, post table strcuture, sample data and the result you want. If you tried writing query, post the full query you used

Madhivanan

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

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-09-02 : 02:22:41
This one can also be resolved with a derived table. Here's an example:

SELECT AliasedColumn, SUM(Column2) AS SumColumn2
FROM
(SELECT AliasedColumn = CASE WHEN Column1 = 1 THEN 99 ELSE 88 END, Column2 FROM SomeTable) t
GROUP BY AliasedColumn

Tara
Go to Top of Page
   

- Advertisement -