Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9017137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:11:20+00:00 2026-06-16T04:11:20+00:00

select case location_id when 1 then ‘DELHI’ when 2 then ‘AHMEDABAD’ when 4 then

  • 0
select 
    case location_id 
      when 1 then 'DELHI' when 2 then 'AHMEDABAD' 
      when 4 then 'HYDERABAD' when 5 then 'KOLKATA' when 6 then 'BANGALORE'
      when 7 then 'MUMBAI' when 8 then 'CHENNAI' 
   end as CITY,
   count(*) as Total
from #tmptab1
group by CITY

I get error

Msg 207, Level 16, State 1, Line 12
Invalid column name ‘CITY’

How to rectify this ? Please help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:11:22+00:00Added an answer on June 16, 2026 at 4:11 am

    You can’t use column aliases in GROUP BY clauses. Please see Conceptual Order of Evaluation of a Select Statement, where you will see that the clauses are (logically) evaluated in this order: FROM, WHERE, GROUP BY, HAVING, SELECT, DISTINCT, UNION, ORDER BY.

    That is not exactly how the engine performs the operation, but it is a useful heuristic for a solid practical understanding of why you can’t use something from the SELECT statement in the GROUP BY clause — it isn’t logically available to the GROUP BY clause as it is evaluated later.

    There are several ways around this:

    1. Repeat the entire expression in the GROUP BY clause:

      SELECT
         City =
            CASE location_id when 1 then 'DELHI' when 2 then 'AHMEDABAD' 
            when 4 then 'HYDERABAD' when 5 then 'KOLKATA' when 6 then 'BANGALORE'
            when 7 then 'MUMBAI' when 8 then 'CHENNAI'
            END,
         Total = Count(*)
      FROM #tmptab1
      GROUP BY
         CASE location_id when 1 then 'DELHI' when 2 then 'AHMEDABAD' 
         when 4 then 'HYDERABAD' when 5 then 'KOLKATA' when 6 then 'BANGALORE'
         when 7 then 'MUMBAI' when 8 then 'CHENNAI'
         END
      
    2. Use a derived table:

      SELECT
         City,
         Total = Count(*) 
      FROM
         (
            SELECT
               City =
                  CASE location_id when 1 then 'DELHI' when 2 then 'AHMEDABAD' 
                  when 4 then 'HYDERABAD' when 5 then 'KOLKATA' when 6 then 'BANGALORE'
                  when 7 then 'MUMBAI' when 8 then 'CHENNAI'
                  END
            FROM #tmptab1
         ) Cities
      GROUP BY City;
      
    3. Use a Common Table Expression (CTE), SQL Server 2005 and up:

      WITH Cities AS (
          SELECT
             City =
                CASE location_id 
                   when 1 then 'DELHI' 
                   when 2 then 'AHMEDABAD' 
                   when 4 then 'HYDERABAD' 
                   when 5 then 'KOLKATA' 
                   when 6 then 'BANGALORE'
                   when 7 then 'MUMBAI' 
                   when 8 then 'CHENNAI'
                END
          FROM #tmptab1
      )
      SELECT
         City,
         Total = Count(*) 
      FROM Cities
      GROUP BY City;
      
    4. Use CROSS APPLY, SQL Server 2005 and up:

      SELECT
         City,
         Total = Count(*)
      FROM
         #tmptab1
         CROSS APPLY (
            SELECT
               City =
                  CASE location_id when 1 then 'DELHI' when 2 then 'AHMEDABAD' 
                  when 4 then 'HYDERABAD' when 5 then 'KOLKATA' when 6 then 'BANGALORE'
                  when 7 then 'MUMBAI' when 8 then 'CHENNAI'
                  END
         ) C
      GROUP BY City;
      

    Since your expression is deterministic, it is possible that you could simply do GROUP BY location_id, however this is not the normal case, and you shouldn’t go around expecting to be able to circumvent normal aggregate grouping logic by selecting a single column, because most of the time such a CASE expression adds value that is not deterministic.

    In fact, because the information is not only deterministic but is about the real world (rather than the business rules) I would recommend that you NOT encode this information in your query! Create a Location table and join to it. It is not best practice to put changeable user data directly into queries–queries are supposed to record process, not content, and what if you add a new location_id? All the queries that use it will have to change. Additionally, grouping by location_id will not work properly if more than one location_id can refer to the same city.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my SQL: SELECT DISTINCT(CASE WHEN state=18 THEN lga ELSE 'Others' END) LGA,
SELECT CASE schuleArtID WHEN 1 THEN SUM(schuleSummeSchueler) / 8 WHEN 2 THEN SUM(schuleSummeSchueler) /
I have this SQL query select case when AllowanceId is null then 2 else
I have executed a code SELECT CASE b.ON_LOAN when 'Y' then 'In Lib' when
How to achieve the following query - SELECT CASE WHEN COUNT(*) > 0 THEN
I currently have a select-case that will read a name field from a recordset,
Background: I have this with rollup query defined in MySQL: SELECT case TRIM(company) when
I'd like to achieve something like this: SELECT (CASE WHEN ...) AS FieldA, FieldA
I'm looking for a template engine like freemarker (with select case directive) for writing
I have the following SQL snippet within a select statement: CASE WHEN wot.id LIKE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.