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 8679511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:59:23+00:00 2026-06-12T20:59:23+00:00

I have a problem to create a custom rownumber with an alias column. Here

  • 0

I have a problem to create a custom rownumber with an alias column. Here are some sample data:

Table question:

id   title   
--   --------    
1     xx
2     xxx

..

Table customerLikeQuestion:

Id   QuestionId CustomerId
---------------------------
1       20          xx
2       100         xx 
xx

Query:

SELECT  q.Id,
        (SELECT     COUNT(*) 
         FROM          dbo.CustomerLikeQuestion
         WHERE      (QuestionId = q.Id)) AS LikeCount
FROM dbo.Question AS q
ORDER BY likecount DESC

The previous query shows:

 id    LikeCount
2136    6
2138    5
2150    5

Now I’d like to put a rownumber to count an incremental order of a row. I tried the following query:

SELECT     TOP (100) PERCENT Id,
           (SELECT     COUNT(*) AS Expr1
            FROM          dbo.CustomerLikeQuestion
            WHERE      (QuestionId = q.Id)) AS LikeCount, 
            row_number() over (order by likecount) as RowNum
FROM         dbo.Question AS q
ORDER BY likecount DESC

But it gives me the following error:

invalid column Likecount.

I do know that Over() cannot work with alias, but how can I get around this problem, either with CTE or subquery, I have not yet come up with any idea. please help.

The right result should be like this:

id  likecount, rownum
----------------------    
xx     6        1
xx     5        2 
xx     4        3
..     0        xx
  • 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-12T20:59:25+00:00Added an answer on June 12, 2026 at 8:59 pm

    Try this instead

    ;WITH LiksCounts
    AS
    (
        SELECT Id,
               (SELECT     COUNT(*) AS Expr1
                FROM          dbo.CustomerLikeQuestion
                WHERE      (QuestionId = q.Id)) AS LikeCount
        FROM         dbo.Question AS q
         GROUP BY Id
    )
    SELECT TOP(100) *, row_number() over (order by likecount) as RowNum
    fROM LiksCounts
    ORDER BY RowNum ASC
    

    Live DEMO

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

Sidebar

Related Questions

I am using jQuery to create custom radio buttons and i have a problem.
I have problem with creating constructor, which Jenkins can call for some JSON data
I have the following Problem: I create a custom control which inherits from an
I have a FragmentActivity where I have written code to create custom title bar
i want to create custom grid column with radio button in it. i have
I have a Maven project multimodule project. Some of the modules create custom packaging
I have problem extracting data of the Oracle custom type from the database. One
I have some problems with my first Open Graph custom action: I've created a
I’m having some problems with validation. I have created a custom validator called RequiredFieldRule
I have a problem to create a preprocessor macro function, that concatenates two Strings

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.