Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
amsqlguy
Yak Posting Veteran
89 Posts |
Posted - 2009-04-15 : 20:17:27
|
| Guys,I have a table in which the sequence is populated from the application and not from the identity property of the SQL Server. For example Empid LastName1 smith2 doe4 bellFor a given input of empid I want to find out what numbers are missing for empid in the above example if I provide the range of empid between 1 and 4, the missing empid should be 3. Is there any effective way to build a function to find out missing number in sequence columnAny suggestion and inputs would helpThanks |
|
|
cshah1
Constraint Violating Yak Guru
347 Posts |
Posted - 2009-04-15 : 20:31:00
|
| a quick search gives you this solutionCreate table #SequenceTable( SeqNumber Int)Declare @Seq intSet @Seq = 10while @Seq <= 100 Begin Insert into #SequenceTable values(@Seq) Set @Seq = @Seq + 1 EndInsert into #SequenceTable values(101)Insert into #SequenceTable values(102)Insert into #SequenceTable values(103)Insert into #SequenceTable values(105)Insert into #SequenceTable values(106)Insert into #SequenceTable values(107)Insert into #SequenceTable values(114)Insert into #SequenceTable values(115)Insert into #SequenceTable values(116)Insert into #SequenceTable values(129)Insert into #SequenceTable values(130)Insert into #SequenceTable values(131)declare @min intdeclare @max intselect @min = min(SeqNumber), @max = max(SeqNumber) from [#SequenceTable]create table #tmp (SeqNumber int)while @min <= @max begin if not exists (select * from [#SequenceTable] where SeqNumber = @min) insert into #tmp (SeqNumber) values (@min) set @min = @min + 1endselect * from #tmpdrop table #tmpHTH |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-04-15 : 22:49:02
|
try this one tooCreate table #SequenceTable( SeqNumber Int)Insert into #SequenceTable values(101)Insert into #SequenceTable values(102)Insert into #SequenceTable values(103)Insert into #SequenceTable values(105)Insert into #SequenceTable values(106)Insert into #SequenceTable values(107)Insert into #SequenceTable values(114)Insert into #SequenceTable values(115)Insert into #SequenceTable values(116)Insert into #SequenceTable values(129)Insert into #SequenceTable values(130)Insert into #SequenceTable values(131)declare @min intdeclare @max intselect @min = min(SeqNumber), @max = max(SeqNumber) from [#SequenceTable]select number from master..spt_values as m where m.type ='p' and number not in (select seqnumber from #SequenceTable) and number between @min and @maxselect number from master..spt_values as m where m.type ='p' and not exists (select * from #SequenceTable where number = seqnumber) and number between @min and @maxselect number from master..spt_values as m left join #SequenceTable s on m.number = s.seqnumberwhere s.seqnumber is nulland m.type ='p' and number between @min and @maxdrop table #SequenceTable |
 |
|
|
LoztInSpace
Aged Yak Warrior
940 Posts |
Posted - 2009-04-16 : 09:58:23
|
| something like this:selectfrom( select row_number() over() as rn from table , table) xleft outer join tableon (table.empId=x.rn) where x.rb is nullwhere rn between @start and @finishcan't work it out exactly - don't have SQL Server here. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-04-16 : 11:16:35
|
| [code]DECLARE @startid int,@endid intSELECT @startid=1,@endid=4 (passed values);With Range (IDVal)AS(SELECT @startidUNION ALLSELECT IDVal+1 FROM Range WHERE IDVal+1<=@endid)SELECT r.IDValFROM Range rLEFT JOIN YourTable tON t.Empid=r.IDValWHERE t.Empid IS NULLOPTION (MAXRECURSION 0)[/code] |
 |
|
|
kunal.mehta
Yak Posting Veteran
83 Posts |
Posted - 2009-04-17 : 02:49:33
|
Hi, Check out this simple onedeclare @table2 table (id int,outDate datetime,outAmount float ) insert into @table2 values (1,'01/01/2009',1)insert into @table2 values (2,'01/01/2009',2)insert into @table2 values (3,'01/01/2009',5)insert into @table2 values (5,'01/2/2009',6)insert into @table2 values (6,'01/3/2009',8.5)select rnum from(select row_number() over (order by id) rnum from @table2) a where rnum not in (select id from @table2)Kunal |
 |
|
|
aprichard
Yak Posting Veteran
62 Posts |
Posted - 2009-04-17 : 03:32:14
|
| declare @StartPoint int, @EndPoint Intset @StartPoint=1set @EndPoint=10declare @table2 table (id int,outDate datetime,outAmount float ) insert into @table2 values (1,'01/01/2009',1)insert into @table2 values (2,'01/01/2009',2)insert into @table2 values (3,'01/01/2009',5)insert into @table2 values (5,'01/2/2009',6)insert into @table2 values (6,'01/3/2009',8.5)insert into @table2 values (10,'01/3/2009',8.5); with CTERange(Num)as(select @StartPoint union allSelect Num+1 from CTERange where num <@EndPoint)select Num from CTERange where num not in (select id from @table2) |
 |
|
|
|
|
|
|
|