今天面试的一道题求解

smilebrid 2008-03-28 03:29:22
今天面试,有道SQL题,到最后居然说我写错了,所以来这里求个解。

现有一数据表A,要求得到第31条至第40条的数据,表中ID字段为自动增长列,但有可能不连续。

我的答案: select * top 10 from (select * top 40 from a) order by id desc

不知哪里错了?或者各位有其它更好的方法麻烦指点一下吧。
...全文
346 35 打赏 收藏 转发到动态 举报
写回复
用AI写文章
35 条回复
切换为时间正序
请发表友善的回复…
发表回复
yudi010 2008-04-01
  • 打赏
  • 举报
回复
按一般都是
select top 10 * from a where id not in (select top 30 id from a)
不过楼主的思想很好
就是忘记在后面的from表加一个临时名了 呵呵
不过没事 加油
林g 2008-04-01
  • 打赏
  • 举报
回复
select top 10 * from a where id not in (select top 30 id from a)
hanlilong 2008-03-31
  • 打赏
  • 举报
回复
我错了,我有工具,发错


select top 100 percent *
from
(select top 10 TOP_A.*
from ( select TOP 40 ID
from A
order by ID) TOP_A
order by ID DESC) TOP_B
order by A


hanlilong 2008-03-31
  • 打赏
  • 举报
回复
我搞不懂要那样干嘛

SELECT TOP 10 31 * FROM A


这样不是简单了事,搞什么先查出前面多少条
Belial_2010 2008-03-31
  • 打赏
  • 举报
回复
基础很重要。
Belial_2010 2008-03-31
  • 打赏
  • 举报
回复
基础很重要啊!大哥!
Belial_2010 2008-03-31
  • 打赏
  • 举报
回复
基础很重要啊!大哥!
Houluoxuan_168 2008-03-31
  • 打赏
  • 举报
回复
[Quote=引用 31 楼 hanlilong 的回复:]
我搞不懂要那样干嘛

SELECT TOP 10 31 * FROM A


这样不是简单了事,搞什么先查出前面多少条
[/Quote]

我想问一下这是不是SQL 2005的写法? 2000肯定是不行的.
milizi820 2008-03-30
  • 打赏
  • 举报
回复
现有一数据表A,要求得到第31条至第40条的数据,表中ID字段为自动增长列,但有可能不连续。
select top 10 * from a where id not in (select id top 30 from a )
quan38383812 2008-03-29
  • 打赏
  • 举报
回复
如果你用这个
select top 5 * from jobs where job_id not in(select top 5 job_id from jobs order by job_id asc)
的话 结果就是 这样排的:

6 Managing Editor 140 225
7 Marketing Manager 120 200
8 Public Relations Manager100 175
9 Acquisitions Manager 75 175
10 Productions Manager 75 165
quan38383812 2008-03-29
  • 打赏
  • 举报
回复
同意22楼的
quan38383812 2008-03-29
  • 打赏
  • 举报
回复
楼主 :
楼主啊,你们的答案有问题列....
应该是:
select top 5 * from (select top 10 * from jobs order by job_id asc) b order by job_id desc
go

10 Productions Manager 75 165
9 Acquisitions Manager 75 175
8 Public Relations Manager 100 175
7 Marketing Manager 120 200
6 Managing Editor 140 225


这样的吧
Aslan_ 2008-03-29
  • 打赏
  • 举报
回复
[Quote=引用楼主 smilebrid 的帖子:]
今天面试,有道SQL题,到最后居然说我写错了,所以来这里求个解。

现有一数据表A,要求得到第31条至第40条的数据,表中ID字段为自动增长列,但有可能不连续。

我的答案: select * top 10 from (select * top 40 from a) order by id desc

不知哪里错了?或者各位有其它更好的方法麻烦指点一下吧。
select * top 10 from (select * top 40 from a order by id desc) as temptable order by id asc
[/Quote]
fa_ge 2008-03-29
  • 打赏
  • 举报
回复
我想这题的本意是要面试者先求记录数,再把这31-40之间的id找出来,而不是直接就把31-40就当成id
fa_ge 2008-03-29
  • 打赏
  • 举报
回复
现有一数据表A,要求得到第31条至第40条的数据,表中ID字段为自动增长列,但有可能不连续。

我的答案: select * top 10 from (select * top 40 from a) order by id desc


------------------------

既然楼主说可能不连续,那么你怎么就知道 从第31条到第40条之间共 10条记录呢?
如果你肯定是10的话,那还要这样写吗?这样写不好吗 select * from t where id between 31 and 40

这个题本来就有问题,上面的回复都把 第31和第40条就当成 它的 id了,这个表id不连续可能是下面这种情况
id num
1 1
5 2
10 3
31 4
40 5
... ...
xx 31
... ...
yy 40
... ...

num是指第多个行。现在求取第5条到第8条记录


create table #(num int identity(1,1),id int)
insert into #
select 1 union all
select 5 union all
select 10 union all
select 30 union all
select 31 union all
select 35 union all
select 50 union all
select 60 union all
select 80


select id from # a
where exists(select 1 from # where id<=a.id having count(1) between 5 and 8)
/*
id
-----------
31
35
50
60

(所影响的行数为 4 行)
*/



如果说该题是这样说的:
现有一数据表A,要求得到id从第31条取后10条的话数据,表中ID字段为自动增长列,但有可能不连续。

用一楼的方法就ok了。
午夜还在张 2008-03-29
  • 打赏
  • 举报
回复
select top 10 * from tablename where id not in (select top 30 id from tablename)
wpeng8218 2008-03-29
  • 打赏
  • 举报
回复
晕,你写的语句语法有问题,不能执行的,从记录集合做数据查询的时候,记录集合需要设置别名!
denyanfeng520 2008-03-29
  • 打赏
  • 举报
回复
第31条至第40条的数据
SELECT TOP (40-31+1) * FROM SA301M WHERE AUTONUM NOT IN (SELECT TOP (31-1) AUTONUM FROM SA301M )
utpcb 2008-03-28
  • 打赏
  • 举报
回复
总结:
select top 10 * from tab
where id not in (select top 30 id from tab order by id)
order by id


select top m * from tablename where id not in (select top n-1 id from tablename)
WITH OrderById AS (SELECT *, ROW_NUMBER() OVER (ORDER BY [ID]) AS RowId FROM a )
SELECT * FROM OrderById WHERE RowId between 30 and 40;
阿甘在杭州 2008-03-28
  • 打赏
  • 举报
回复
环境:sql server 2005:
select * from (select row_number() over (order by id desc) num,* from a) as tb
where tb.num between 31 and 40
环境:sql server 2000:
declare @tb table(idx int identity(1,1),Id int)
insert @tb(id)
select id from a order by id desc
select a.* from a,@tb t
where a.id=t.id and t.idx between 31 and 40 order by t.idx

就ok了
加载更多回复(14)
此为我个人搜集整理的, 精选微软等公司数据结构和算法的面试100[前41-60], 此绝对值得你下载收藏。 ----------------------------- 网友yui评论,真是够多的了,从此,不用再看其它面试.... 一句话,请享用。 July、2010/11.05. ----------------------------------------------- 其它资源,下载地址: [最新整理公布][汇总II]微软等数据结构+算法面试100[第1-80] http://download.csdn.net/source/2846055 1.[最新答案V0.3版]微软等数据结构+算法面试100[第21-40答案] http://download.csdn.net/source/2832862 2.[第1-60汇总]微软等数据结构+算法面试100 http://download.csdn.net/source/2826690 3.[答案V0.2版]精选微软数据结构+算法面试100[前20]--修正 http://download.csdn.net/source/2813890 //此份答案是针对最初的V0.1版本,进行的校正与修正。 4.[答案V0.1版]精选微软数据结构+算法面试100[前25] http://download.csdn.net/source/2796735 5.[第二部分]精选微软等公司结构+算法面试100[前41-60]: http://download.csdn.net/source/2811703 6.[第一部分]精选微软等公司数据结构+算法经典面试100[1-40] http://download.csdn.net/source/2778852 更多资源,下载地址: http://v_july_v.download.csdn.net/ ------------------------------------------------------ 各位,若对以上100任何一道,或对已上传的任何一的答案, 有任何问,请把你的思路、想法,回复到此帖子上, 微软等100系列,永久维护地址(2010年11.26日): http://topic.csdn.net/u/20101126/10/b4f12a00-6280-492f-b785-cb6835a63dc9.html

27,581

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧