我有两个表emp1和emp2,我想从表中获取备用记录,如奇数和偶数以及Emp的第三个Max Salary
表格如emp1
empno empname empposition
1 abc Manager
2 cef Clark
3 xyz Supervisor
4 pqr Peon
5 hij Senior Manager
像emp2一样的表
empno empname empsalary
1 abc 25,000
2 cef 15,000
3 xyz 10,000
4 pqr 5,000
5 hij 35,000
答案 0 :(得分:1)
在SQL表中获取奇数行和偶数行的最佳方法是使用RowNumber完成,
Declare @Emp2 Table (empno INT, empname VARCHAR(10), empsalary INT)
INSERT INTO @Emp2 (empno,empname,empsalary) VALUES (1 , 'abc' , 25000)
INSERT INTO @Emp2 (empno,empname,empsalary) VALUES (2 , 'cef' , 15000)
INSERT INTO @Emp2 (empno,empname,empsalary) VALUES (3 , 'xyz' , 10000)
INSERT INTO @Emp2 (empno,empname,empsalary) VALUES (4 , 'pqr' , 5000 )
INSERT INTO @Emp2 (empno,empname,empsalary) VALUES (5 , 'hij' , 35000)
SELECT *
FROM (
SELECT *, Row_Number() OVER(ORDER BY empno) AS RowNumber
--Row_Number() starts with 1
FROM @Emp2
) t
WHERE t.RowNumber % 2 = 0 --Even
--WHERE t.RowNumber % 2 = 1 --Odd
答案 1 :(得分:1)
使用偶数员工的Bellow Query
select empname as Employee,salary as Salary
from emp2
where empno in (
select empno
from emp2
group by empno having empno%2=0
)
同时使用Bellow Query for Odd Number Employee
select empname as Employee,salary as Salary
from emp2
where empno in (
select empno
from emp2
group by empno having empno%2 <> 0
)
答案 2 :(得分:1)
您可以尝试以下方式,它对我有用。
获取连续编号的行:
SELECT * FROM table_name WHERE column_name%2 = 0
获取奇数编号的行:
SELECT * FROM table_name WHERE column_name%2 = 1
希望它有所帮助!!
答案 3 :(得分:0)
试试这个:
FOR ODD RECORDS
SELECT * FROM EMP1 INNER JOIN EMP2 ON EMP1.EMPNO=EMP2.EMPNO WHERE (EMP1.empno %2)<> 0
即使是有记录的
SELECT * FROM EMP1 INNER JOIN EMP2 ON EMP1.EMPNO=EMP2.EMPNO WHERE (EMP1.empno %2)= 0
FOR THIRD MAX SALARY
SELECT TOP 1 EMPSALARY FROM EMP2 WHERE EMPSALARY NOT IN
(SELECT TOP 2 EMPSALARY FROM EMP2 ORDER BY EMPSALARY DESC)
ORDER BY EMPSALARY DESC
答案 4 :(得分:0)
我试试这个,它对我有用,我希望它能帮助你。
(偶数)
select * from emp1 where rowid in (select decode(mod(rownum,2),0,rowid, null) from emp1);
(ODD NUMBERED)
select * from emp1 where rowid in (select decode(mod(rownum,2),0,null ,rowid) from emp1);
第三名MAX薪水
select distinct empsalary from emp2 e1 where 3 = (select count(distinct empsalary ) from emp2 e2 where e1.empsalary <= e2.empsalary );
答案 5 :(得分:0)
要从表中获取备用记录,可以运行以下查询:
select r,empno,empname,empposition from(select rownum r,empno,empname,empposition from emp1) where mod(r,2)=1;
答案 6 :(得分:0)
下面是在SQL SERVER中感染奇数记录的记录
这是salarytable2
:
name salary
abc 100000
bc 1000000
efg 50000
klm 2000000
在这里,我使用Row_number()
获取行号,并对其应用模数以将其分类为偶数或奇数记录。
select * from (select *,ROW_NUMBER() over(order by salary) as rownum from salarytable2) as saltable where saltable.rownum%2 <>0;
答案 7 :(得分:0)
WITH CTE_GET_ALTERNATE_RECORDS
AS
(
SELECT *, ROW_NUMBER()OVER(ORDER BY BOOK_ID) AS [ROW_NUMBER] FROM BOOK
)
SELECT * FROM CTE_GET_ALTERNATE_RECORDS WHERE [ROW_NUMBER]%2=1
--WHERE [ROW_NUMBER]%2=0 For even rows
答案 8 :(得分:0)
获取奇数记录:
select columnname from (select rowno,columnname from table) where mod(rowno,2)=1;
获取偶数记录:
select columnname from (select rowno/columnname from table) where mod(rowno,2)=0;