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
415 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
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
 
LVL 16

Accepted Solution

by:
vdr1620 earned 500 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now