How to rewrite Case statement in Group By

Sql server handles this OK (Northwind example), but I'm passing this to another database system that does not allow it in Group By clauses.

How could this be rewritten without the select case in the Group By?

Thx


SELECT
  CASE WHEN c.CompanyName Like 'a%' THEN 'Begins with A' ELSE c.CompanyName END
FROM Customers c
  GROUP BY
  CASE WHEN c.CompanyName Like 'a%' THEN 'Begins with A' ELSE c.CompanyName END
  ORDER BY
  CASE WHEN c.CompanyName Like 'a%' THEN 'Begins with A' ELSE c.CompanyName END
LVL 8
TZychAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

g_johnsonCommented:
try assigning an alias to the Case ... End column and grouping by that
doesn't work in MS SQL but may on other database

SELECT
   CASE WHEN blah blah THEN whatever ELSE sometingelse END [A]
   GROUP BY A <--- DOESN'T WORK IN MSSQL, don't know why!
   ORDER BY A  <--- WORKS IN MSSQL
0
TZychAuthor Commented:
thanks. good idea..but it didn't work
0
J2811Commented:
Well when all things fails, do this

SELECT GIVEMEANAME
FROM (
SELECT
  CASE WHEN c.CompanyName Like 'a%' THEN 'Begins with A' ELSE c.CompanyName END GIVEMEANAME
FROM Customers c ) A
GROUP BY GIVEMEANAME
ORDER BY GIVEMEANAME
;

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.