SQL IMP Queries
Q:Find the missing numbers
create table MSeq
( id int
)
select * from MSeq
Declare @minnum int
Declare @maxnumber int
select @maxnumber=max(ID)from MSeq
select @minnum=min(ID) from MSeq
--Create table #tblSeq
--(
-- id int
--)
Declare @count int
set @count=1
--select id into #tblSeq from MSeq
begin
while(@count<=@maxnumber)
begin
insert into #tblSeq values(@count)
set @count=@count+1
end
end
select M.id from #tblSeq M
left join MSeq S on S.id=M.id
where S.id is null
create table MSeq
( id int
)
select * from MSeq
Declare @minnum int
Declare @maxnumber int
select @maxnumber=max(ID)from MSeq
select @minnum=min(ID) from MSeq
--Create table #tblSeq
--(
-- id int
--)
Declare @count int
set @count=1
--select id into #tblSeq from MSeq
begin
while(@count<=@maxnumber)
begin
insert into #tblSeq values(@count)
set @count=@count+1
end
end
select M.id from #tblSeq M
left join MSeq S on S.id=M.id
where S.id is null
Comments
Post a Comment