how to show only even or odd rows in sql server 20

2020-01-31 01:00发布

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条回答
Viruses.
2楼-- · 2020-01-31 01:46

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; 
查看更多
成全新的幸福
3楼-- · 2020-01-31 01:47

To fetch even records

select *
from (select id,row_number() over (order by id) as r from table_name) T
where mod(r,2)=0;

To fetch odd records

select *
from (select id,row_number() over (order by id) as r from table_name) T
where mod(r,2)=1;
查看更多
成全新的幸福
4楼-- · 2020-01-31 01:47

for SQL > odd:

select * from id in(select id from employee where id%2=1)

for SQL > Even:

select * from id in(select id from employee where id%2=0).....f5
查看更多
劳资没心,怎么记你
5楼-- · 2020-01-31 01:48
  SELECT * FROM (SELECT ROW_NUMBER () OVER (ORDER BY sal DESC) row_number, sr,sal FROM empsal) a WHERE (row_number%2) = 1

and

      SELECT * FROM (SELECT ROW_NUMBER () OVER (ORDER BY sal DESC) row_number, sr,sal FROM   empsal) a WHERE (row_number%2) = 0
查看更多
走好不送
6楼-- · 2020-01-31 01:49

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.

查看更多
我命由我不由天
7楼-- · 2020-01-31 01:50

odd number query:

SELECT *
  FROM   ( SELECT rownum rn, empno, ename
           FROM emp
         ) temp
  WHERE  MOD(temp.rn,2) = 1

even number query:

SELECT *
  FROM   ( SELECT rownum rn, empno, ename
           FROM emp
         ) temp
  WHERE  MOD(temp.rn,3) = 0
查看更多
登录 后发表回答