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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:18:27+00:00 2026-06-11T22:18:27+00:00

I have to migrate some SQL from PostgreSQL to SQL Server (2005+). On PostgreSQL

  • 0

I have to migrate some SQL from PostgreSQL to SQL Server (2005+). On PostgreSQL i had:

select count(id) as count, date
from table 
group by date 
order by count 
limit 10 offset 25

Now i need the same SQL but for SQL Server. I did it like below, but get error: Invalid column name 'count'. How to solve it ?

select * from (
   select row_number() over (order by count) as row, count(id) as count, date 
   from table 
   group by date
) a where a.row >= 25 and a.row < 35
  • 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-11T22:18:28+00:00Added an answer on June 11, 2026 at 10:18 pm

    You can’t reference an alias by name, at the same scope, except in an ending ORDER BY (it is an invalid reference inside of a windowing function at the same scope).

    To get the exact same results, it may need to be extended to (nesting scope for clarity):

    SELECT c, d FROM 
    (
      SELECT c, d, ROW_NUMBER() OVER (ORDER BY c) AS row FROM 
      (
        SELECT d = [date], c = COUNT(id) FROM dbo.table GROUP BY [date]
      ) AS x
    ) AS y WHERE row >= 25 AND row < 35;
    

    This can be shortened a little bit as per mohan’s answer.

    SELECT c, d FROM 
    (
      SELECT COUNT(id), [date], ROW_NUMBER() OVER (ORDER BY COUNT(id))
       FROM dbo.table GROUP BY [date]
    ) AS y(c, d, row)
    WHERE row >= 25 AND row < 35;
    

    In SQL Server 2012, it’s much easier with OFFSET / FETCH – closer to the syntax you’re used to, but actually using ANSI-compatible syntax rather than proprietary voodoo.

    SELECT c = COUNT(id), d = [date]
    FROM dbo.table GROUP BY [date]
    ORDER BY COUNT(id)
    OFFSET 25 ROWS FETCH NEXT 10 ROWS ONLY;
    

    I blogged about this functionality in 2010 (lots of good comments there too) and should probably invest some time doing some serious performance tests.

    And I agree with @ajon – I hope your real tables, columns and queries don’t abuse reserved words like this.

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

Sidebar

Related Questions

I have to migrate some legacy data from stand-alone sql server database to sharepoint
I need to manually migrate modified stored procedures from a DEV SQL Server 2005
I have an old program pushing some data from a Microsoft SQL server database
I currently have a SQL Server (Express 2005) database to hold some transaction/metadata that
We have recently migrated some 30 DTS packages in SQL Server 2000 to SSIS
I want to migrate to Windows 7 from XP, and I have some questions:
I need to migrate data in a large flat table located in SQL Server
I'm migrating SPs from Sybase to SQL Server and I have a case where
I have a need to migrate game score table from (don't laugh please...) *.ini
I have written a T-SQL script that migrates some data from one database to

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.