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

  • Home
  • SEARCH
  • 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 9097907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:14:01+00:00 2026-06-17T00:14:01+00:00

I would like to use CASE statement in SELECT. I select from user table,

  • 0

I would like to use CASE statement in SELECT.

I select from user table, and (as one attribute) I also use nested SQL:

SELECT 
   registrationDate, 
   (SELECT COUNT(*) FROM Articles WHERE userId = Users.userId) as articleNumber, 
   hobbies, ...
FROM USERS

and then I would like to do a CASE statement to get rank of user (rank is dependent on articleNumber).

I tried like this:

SELECT 
   registrationDate, 
   (SELECT COUNT(*) FROM Articles WHERE Articles.userId = Users.userId) as articleNumber, 
   ranking =
      CASE
         WHEN articleNumber < 2 THEN 'Ama'
         WHEN articleNumber < 5 THEN 'SemiAma' 
         WHEN articleNumber < 7 THEN 'Good'  
         WHEN articleNumber < 9 THEN 'Better' 
         WHEN articleNumber < 12 THEN 'Best'
         ELSE 'Outstanding'
      END,
   hobbies, etc...
FROM USERS

Parsing displays no error, but when I try to run it I get error:

Msg 207, Level 16, State 1, Procedure GetUserList, Line XY
Invalid column name ‘articleNumber’.

CASE doesn’t “recognize” my nested SELECT, I guess.

I helped myself with some other solutions like
SQL Server 2008 – Case / If statements in SELECT Clause
but nothing seems to work.

I also didn’t find any similar problem with ‘<‘ and ‘>’ comparison.

Any help would be greatly appreciated 😉

  • 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-17T00:14:03+00:00Added an answer on June 17, 2026 at 12:14 am

    Please select the same in the outer select. You can’t access the alias name in the same query.

    SELECT *, (CASE
            WHEN articleNumber < 2 THEN 'Ama'
            WHEN articleNumber < 5 THEN 'SemiAma' 
            WHEN articleNumber < 7 THEN 'Good'  
            WHEN articleNumber < 9 THEN 'Better' 
            WHEN articleNumber < 12 THEN 'Best'
            ELSE 'Outstanding'
            END) AS ranking 
    FROM(
        SELECT registrationDate, (SELECT COUNT(*) FROM Articles WHERE Articles.userId = Users.userId) as articleNumber, 
        hobbies, etc...
        FROM USERS
    )x
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, I would like to use a CASE STATEMENT for this, but I am
I would like to have an UPDATE statement like this: SELECT * FROM Employee
Hi I would like to write one SQL statement that returns both the count
I would like to use a table alias within the projection of another select
I would like to add an additional parameter in my url, the use case
I would like to use the logout function from Django but not sure how
Having a problem with a WHERE search statement would like to use a construction
I have a SELECT statement which I would like to optimize. The mysql -
I have an array of Integers in Java, I would like use only a
I would like to use R to extract the speaker out of scripts formatted

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.