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

前端 未结 16 1436
甜味超标
甜味超标 2020-12-05 13:53

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 ?

thank\'s in advance

相关标签:
16条回答
  • 2020-12-05 14:57

    Oracle Database

    ODD ROWS

    select * from (select mod(rownum,2) as num , employees.* from employees) where num =0;
    

    EVEN ROWS

    select * from (select mod(rownum,2) as num , employees.* from employees) where num =1; 
    
    0 讨论(0)
  • 2020-12-05 15:00

    Try this :

    odd :

    select * from( 
    SELECT col1, col2, ROW_NUMBER() OVER(ORDER BY col1 DESC) AS 'RowNumber', 
    FROM table1
    ) d where (RowNumber % 2) = 1 
    

    even :

    select * from( 
    SELECT col1, col2, ROW_NUMBER() OVER(ORDER BY col1 DESC) AS 'RowNumber', 
    FROM table1
    ) d where (RowNumber % 2) = 0
    
    0 讨论(0)
  • 2020-12-05 15:00

    Here’s a simple and straightforward answer to your question, (I think). I am using the TSQL2012 sample database and I am returning only even or odd rows based on “employeeID” in the “HR.Employees” table.

    USE TSQL2012;
    GO
    

    Return only Even numbers of the employeeID:

    SELECT *
    FROM HR.Employees
    WHERE (empid % 2) = 0;
    GO
    

    Return only Odd numbers of the employeeID:

    SELECT *
    FROM HR.Employees
    WHERE (empid % 2) = 1;
    GO
    

    Hopefully, that’s the answer you were looking for.

    0 讨论(0)
  • 2020-12-05 15:00

    Try following

    SELECT * FROM Worker WHERE MOD (WORKER_ID, 2) <> 0;
    
    0 讨论(0)
提交回复
热议问题