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
420 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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
 

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

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.

Question has a verified solution.

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

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.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

810 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