查詢表的缺失範圍

2021-04-23 11:54:43 字數 1246 閱讀 1046

有表如下

go/****** 物件:  table [dbo].[t1]    指令碼日期: 12/21/2008 22:29:59 ******/

set ansi_nulls on

goset quoted_identifier on

gocreate table [dbo].[t1](

[col1] [int] not null,

primary key clustered

([col1] asc

)with (ignore_dup_key = off) on [primary]

) on [primary]

insert into dbo.t1

select 1 union all

select 2 union all

select 3 union all

select 100 union all

select 101 union all

select 103 union all

select 104 union all

select 105 union all

select 106

現在查詢表的缺失範圍

如以上表的缺失範圍是

begn_col      end_col

4                  99

102             102

如下是該問題的解決方法

--author:cowge

------方法一

select col1+1 as begin_col,

end_col=(

select min(col1) from dbo.t1 where col1>a.col1

)-1from dbo.t1 as a

where not exists

(select * from dbo.t1 where col1=a.col1+1

) and col1<(select max(col1) from dbo.t1)

-----方法二

select cur+1 as begin_col,nxt-1 as end_col from

(select col1 as cur,

nxt=(

select min(col1) from dbo.t1 where col1>a.col1

) from dbo.t1 as a

) t where nxt-cur>1

查詢缺失的數

有這樣乙個陣列,它包含 1 n 中的 n 1 個數,缺少的那個數不知道,怎樣才能找到那個數?這裡有兩種方法實現上述問題。1.對所有數求和之後用原有的總和去減即可 2.利用異或運算 由於異或的原理是相同則為0,所以,首先異或1到n中的每個數,接著利用所得到的結果異或陣列中的 n 1 個數,最終得到的那...

查詢缺失的整數

一 有99個不重複的正整數組成的無序陣列,數值範圍是1 100,求出缺失的乙個1 100中的正整數。解 1.對1 100這100個數字求和,得到sum 2.用sum依次減去無序陣列中的每個值,最後sum的值就是缺失的那個正整數。public static int findnum int arr,in...

HDU 6288 缺失的資料範圍

題目 思路簡單,wa題無數遍,哭了 對了個拍,發現log2不可以用log來求,因為不能保證精度,預處理了2的倍數用二分找了。include include include include include include include include include include define g...