sql语句

hqs19821108 2010-07-30 04:32:23
表A
字段1
值:1,2,3,4,5,7,8,9,10,11,13,14,15……100
我想从表A的字段1中把没有连续加1的值取出来,比如 5和7之前没有6,就把6取出来,
11和13之间没有12,就把12取出来……这个sql怎么写呢
...全文
146 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
xyj052 2010-07-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 maco_wang 的回复:]
SQL code

declare @表A table (字段1 int)
insert into @表A
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 7 union all
select 8 union all
se……
[/Quote]

这个做法实际上就做个了顺序数字的临时表,对临时表进行比较
xyytuo 2010-07-30
  • 打赏
  • 举报
回复
hokor 2010-07-30
  • 打赏
  • 举报
回复
declare @tb table (col varchar(1000))
insert into @tb (col) values('1,2,3,4,5,7,8,9,10,11,13,14,15,20')
--select * from @tb
--col
--1,2,3,4,5,7,8,9,10,11,13,14,15,20
;with tb as (
SELECT row_number()OVER( ORDER BY col)rn,col=convert(int,SUBSTRING(a.col,b.number,CHARINDEX(',',a.col+',',b.number)-b.number) )
FROM @tb a,master..spt_values b
WHERE b.number<=LEN(a.col+'a')
AND CHARINDEX(',',','+a.col,b.number)=b.number
AND b.type = 'P'
)
SELECT b.number from tb t right join master..spt_values b on t.col = b.number
,(select min(col) [min],max(col) [max] from tb) c
where b.type = 'P' and t.col is null and b.number between c.[min] and c.[max]
--只统计给定值最小值和最大值之间的值不连续值,不能统计超过2047的值
--若要统计大约2047的值需要创建一个连续整数表带起系统表master..spt_values,最大值根据统计范围确定。
/*
number
6
12
16
17
18
19
*/
Andy__Huang 2010-07-30
  • 打赏
  • 举报
回复

declare @表A table (col int)
insert into @表A
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 7 union all
select 8 union all
select 9 union all
select 10 union all
select 11 union all
select 13 union all
select 14 union all
select 15

select *
from (select distinct colorder from syscolumns)a
where colorder not in (select col from @表A) and colorder<=(select max(col) from @表A)

--colorder
---------------
6
12


hqs19821108 2010-07-30
  • 打赏
  • 举报
回复
2楼的高手能解释一下sQL语句啊
xmx2009 2010-07-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 maco_wang 的回复:]
SQL code

declare @表A table (字段1 int)
insert into @表A
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 7 union all
select 8 union all
se……
[/Quote]
hqs19821108 2010-07-30
  • 打赏
  • 举报
回复
只要不是连续的都取出来
jstoic 2010-07-30
  • 打赏
  • 举报
回复
问题
1.数字只是从1到100?
2.这些数字都是用,号间隔,并存在同一个字段中?
3.1,2,3,6。。。需不需要把45都取出来?如果间隔更大的呢?
叶子 2010-07-30
  • 打赏
  • 举报
回复

declare @表A table (字段1 int)
insert into @表A
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 7 union all
select 8 union all
select 9 union all
select 10 union all
select 11 union all
select 13 union all
select 14 union all
select 15

declare @表b table(字段2 int)
declare @t int
select @t= min(字段1) from @表A
while (@t<(select max(字段1) from @表A))
begin
insert into @表b select @t
set @t=@t+1
end

select b.* from @表b b left join @表A a
on b.字段2=a.字段1 where a.字段1 is null

/*
字段2
-----------
6
12
*/

永生天地 2010-07-30
  • 打赏
  • 举报
回复
结合

[sql server] 各种字符串拆分函数 及 实例
http://blog.csdn.net/xys_777/archive/2010/06/21/5684377.aspx

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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