Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I return only the records that are between rowID 50 and 150 with this Select Statement

Posted on 2010-08-17
8
Medium Priority
?
425 Views
Last Modified: 2012-05-10
I am trying to return only the 100 rows between the RowIDs of 50 and 150 from the following statement I get an error  of :

Msg 207, Level 16, State 1, Line 12
Invalid column name 'RowID'.
Msg 207, Level 16, State 1, Line 12
Invalid column name 'RowID'.
What is up with this?

SELECT
      ROW_NUMBER() over(order by [DBName],[D_ROW_ID] desc) RowID
      ,[DBName]
      ,[D_ROW_ID]
      ,[ASSETINDEX]
      ,[TRANSDATESTAMP]
      ,[TRANSTIMESTAMP]
      ,[FISCALYRADDED]
      ,[FAYEAR]
      ,[FAPERIOD]
  FROM [CUSTOM].[dbo].[test_TRANS]
  where RowID < 150 and RowID > 50
GO
0
Comment
Question by:Steve Samson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 4

Expert Comment

by:justin-clarke
ID: 33455772
Is your RowID column called D_ROW_ID ?

If so try...

WHERE ((D_ROW_ID < 150) AND (D_ROW_ID > 50))
0
 
LVL 18

Expert Comment

by:DarrenD
ID: 33455778
Hi,
Are you missing "as"

ROW_NUMBER() over(order by [DBName],[D_ROW_ID] desc) as RowID
0
 
LVL 3

Expert Comment

by:mnachu
ID: 33455798
One solution is to use CTE and get all rows into the CTE with the RowID as u have done.

After that use the Where clause and filter from the CTE.

Regards,
Nachi
0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
LVL 16

Accepted Solution

by:
vdr1620 earned 2000 total points
ID: 33455799
You cannot use the derived column in the same Where It should rather be
SELECT * FROM
(
SELECT
      ROW_NUMBER() over(order by [DBName],[D_ROW_ID] desc) RowID
      ,[DBName]
      ,[D_ROW_ID]
      ,[ASSETINDEX]
      ,[TRANSDATESTAMP]
      ,[TRANSTIMESTAMP]
      ,[FISCALYRADDED]
      ,[FAYEAR]
      ,[FAPERIOD]
  FROM [CUSTOM].[dbo].[test_TRANS]
  )A
where RowID < 150 and RowID > 50
GO
0
 

Author Closing Comment

by:Steve Samson
ID: 33456648
THis worked flawlessly for what i was trying to accomplish
0
 
LVL 18

Expert Comment

by:DarrenD
ID: 33459207
Hi,

Just wondering. Would this work too. It's a single select so it could be faster! (if your selecting a lot of data). Two selects seems a little much for what you are trying to accomplish.

     SELECT
      ROW_NUMBER() over(order by [DBName],[D_ROW_ID] desc) RowID
      ,[DBName]
      ,[D_ROW_ID]
      ,[ASSETINDEX]
      ,[TRANSDATESTAMP]
      ,[TRANSTIMESTAMP]
      ,[FISCALYRADDED]
      ,[FAYEAR]
      ,[FAPERIOD]
  FROM [CUSTOM].[dbo].[test_TRANS]
where ROW_NUMBER() over(order by [DBName],[D_ROW_ID] desc) < 150 and ROW_NUMBER() over(order by [DBName],[D_ROW_ID] desc) > 50

Or...
Declare @rowID int
     SELECT
      @rowID  = ROW_NUMBER() over(order by [DBName],[D_ROW_ID] desc) RowID
      ,[DBName]
      ,[D_ROW_ID]
      ,[ASSETINDEX]
      ,[TRANSDATESTAMP]
      ,[TRANSTIMESTAMP]
      ,[FISCALYRADDED]
      ,[FAYEAR]
      ,[FAPERIOD]
  FROM [CUSTOM].[dbo].[test_TRANS]
where @rowID  < 150 and @rowID  > 50

Just an after thought....haven't tried it.

Darren
0
 
LVL 16

Expert Comment

by:vdr1620
ID: 33459303
Darren, the 1st SQL would sure work.. bu the 2nd SQL might end up giving an error as you cannot assign values with data retrieval oparations
0
 
LVL 18

Expert Comment

by:DarrenD
ID: 33459463
Nice one.
0

Featured Post

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question