How to display only Even numbered rows from a table?

————————————————————————–
USE [ADVENTUREWORKS2012]
GO
————————————————————————–
— Fetching only Even numbered rows.
————————————————————————–

SELECT [Even].[BusinessEntityID], [Even].[PhoneNumber],
              [Even].[PhoneNumberTypeID], [Even].[ModifiedDate]
FROM (
                        SELECT [BusinessEntityID], [PhoneNumber],
                                      [PhoneNumberTypeID],[ModifiedDate],
   Row_Number() OVER(ORDER BY [BusinessEntityID]) AS RN
                                           FROM [Person].[PersonPhone]
) AS [Even]
WHERE [Even].RN % 2 = 0
————————————————————————–

Note:

  • For fetching ODD rows, just replace RN % 2 = 0 with RN % 2 = 1.
  • We can also achieve this by creating a CTE with the sub query above.

 

Leave a Reply

Your email address will not be published. Required fields are marked *