求一个查询语句

cheyaoming 2011-01-11 09:20:29
数据表:ta
序号 姓名
001 张三
002 李四
003 XXX
。 。
。 。
100 XXX
101 XXX
。 。
。 。
中间有可能缺少了某个序号造成序号不连续。例如中间少了一个‘021 ’
如何查询出某两个序号之间(比如001--100)缺少的那个序号。(就是查询出‘021’这个数据表里本来不存在的序号)
...全文
247 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
abuying 2011-01-13
  • 打赏
  • 举报
回复
declare @s varchar(1000)
select @s = coalesce(@s+',' + convert(varchar(10),id),convert(varchar(10),id)) from ta --from (select top 100 percent id from ta order by id) as a
select @s
华夏小卒 2011-01-13
  • 打赏
  • 举报
回复
select top 10000 id=identity(int,1,1) into #tb from sys.objects a,sys.objects b --top 后面的数字自己定

select right('000'+ltrim(id),3) from #tb where right('000'+ltrim(id),3) not in (select 序号 from ta)
insus 2011-01-13
  • 打赏
  • 举报
回复
mugua604 2011-01-11
  • 打赏
  • 举报
回复
create table ##tmp(id varchar(10),nam varchar(30))


insert into ##tmp(id,nam)
select '001','a'
union all
select '002','d'
union all
select '004','b'

select right(1001+number,3) from master.dbo.spt_values a
where not exists(select 1 from ##tmp
where right(1001+number,3)=id)
AND type='P' AND number<10

/*

------
003
005
006
007
008
009
010

(7 行受影响)


*/
mugua604 2011-01-11
  • 打赏
  • 举报
回复
create table ##tmp(id varchar(10),nam varchar(30))


insert into ##tmp(id,nam)
select '001','a'
union all
select '002','d'
union all
select '004','b'

select right(1001+number,3) from master.dbo.spt_values a
where not exists(select 1 from ##tmp
where right(1001+number,3)=id)
AND type='P' AND number<10

/*

------
003
005
006
007
008
009
010

(7 行受影响)


*/
mugua604 2011-01-11
  • 打赏
  • 举报
回复

create table ##tmp(id varchar(10),nam varchar(30))


insert into ##tmp(id,nam)
select '001','a'
union all
select '002','d'
union all
select '004','b'

select right(1001+number,3) from master.dbo.spt_values a
where not exists(select 1 from ##tmp
where right(1001+number,3)=id)
AND type='P' AND number<10
youngzi 2011-01-11
  • 打赏
  • 举报
回复
建一个临时表 @tmp,放进去所有的连续的序号,然后用
select * from @tmp where id not in (select id from table)
飘零一叶 2011-01-11
  • 打赏
  • 举报
回复
drop table ta
CREATE TABLE TA
(
序号 varchar(10),
姓名 nvarchar(20)
)
insert into TA
select '001','张三' union all
select '002','李四' union all
select '003','XXX' union all
select '005','XXX' union all
select '008','XXX' union all
select '010','XXX'

--查询全部断号
select a.n from
(select right('000'+ltrim(number),3) as n from master..spt_values where type='p' and number between (select min(序号) from ta) and (select MAX(序号) from ta)) a left join TA b on a.n=b.序号
where b.序号 is null
/*
-----------
004
006
007
009
*/

--最小断号
select min(a.n) from
(select right('000'+ltrim(number),3) as n from master..spt_values where type='p' and number between (select min(序号) from ta) and (select MAX(序号) from ta)) a left join TA b on a.n=b.序号
where b.序号 is null
/*
----------
(无列名)
004
*/
daishaodong 2011-01-11
  • 打赏
  • 举报
回复
master..spt_values where type='p'
有个问题,最大只有255,序号大于255就不行了。。。

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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