--一樓的寫法不正確的,union中不能2個order by --用臨時表好了 select * into #t1 from (select top 10 * from T order by 工资 desc) A select * into #t2 from (select top 10 * from T order by 工资) Bselect * from #t1 union all select * from #t2
如果9、10、11工资都一样的情况都算是前10的话,还可以用如下的方法 select * from T where (select count(1) from T T1 where T1.salary > T.salary) <= 9 or (select count(1) from T T2 where T2.salary < T.salary) <= 9
建议用临时表 本机:select * from 数据库.dbo.表 远程:SELECT * FROM OPENROWSET('MSDASQL','DRIVER={SQL Server};SERVER=地址;UID=用户名;PWD=密码',数据库.dbo.表) 取他们分别符合的数据再合并一起来查询
再按倒序排一下,再取前十名就是了!!!
--用臨時表好了
select * into #t1 from (select top 10 * from T order by 工资 desc) A
select * into #t2 from (select top 10 * from T order by 工资) Bselect * from #t1
union all
select * from #t2
select * from T where
(select count(1) from T T1 where T1.salary > T.salary) <= 9
or
(select count(1) from T T2 where T2.salary < T.salary) <= 9
本机:select * from 数据库.dbo.表
远程:SELECT * FROM OPENROWSET('MSDASQL','DRIVER={SQL Server};SERVER=地址;UID=用户名;PWD=密码',数据库.dbo.表)
取他们分别符合的数据再合并一起来查询