Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to show only even or odd rows in sql server 2008?

Tags:

i have a table MEN in sql server 2008 that contain 150 rows.

how i can show only the even or only the odd rows ?

like image 877
Gold Avatar asked Jun 08 '10 13:06

Gold


People also ask

What is query to display odd rows from the Employees table?

We use HR schema for showing the odd or even rows from employees table. Employees table has 107 rows and employee_id start from 100 value. We use filter on employee_id <= 110 to get only less output which simplify to show in following example. For Even Rows ( We use Mod function and rownum.


1 Answers

Check out ROW_NUMBER()

SELECT t.First, t.Last FROM (     SELECT *, Row_Number() OVER(ORDER BY First, Last) AS RowNumber              --Row_Number() starts with 1     FROM Table1 ) t WHERE t.RowNumber % 2 = 0 --Even --WHERE t.RowNumber % 2 = 1 --Odd 
like image 165
Matthew Whited Avatar answered Oct 01 '22 17:10

Matthew Whited