Question Findout missed numbers

tqmd1

Well-known member
Joined
Dec 5, 2009
Messages
60
Programming Experience
Beginner
Dear Experts

Sqlserver Table1holds data as follows

date---------------sno
01/01/2010-------1
01/01/2010-------2
01/01/2010-------4
01/01/2010-------5
01/01/2010-------7
02/01/2010-------1
02/01/2010-------2
02/01/2010-------6
02/01/2010-------8

How to findout missed sno in 01/01/2010?

I want to get this result
3
6

Please help
 
VB.NET:
Expand Collapse Copy
--Create test table and fill it
DECLARE @t TABLE(
d datetime,
sno int
)
INSERT @t 
SELECT '20100101',1
UNION ALL SELECT '20100101',2
UNION ALL SELECT '20100101',4
UNION ALL SELECT '20100101',5
UNION ALL SELECT '20100101',7
UNION ALL SELECT '20100102',1
UNION ALL SELECT '20100102',2
UNION ALL SELECT '20100102',6
UNION ALL SELECT '20100102',8

DECLARE @d datetime
SET @d='20100101'
----------------------
SELECT n
FROM numbers n
LEFT join @t t ON n.n=t.sno AND t.d=@d
WHERE n BETWEEN (SELECT min(sno) FROM @t WHERE d=@d)
	 AND (SELECT max(sno) FROM @t WHERE d=@d) 
	AND sno IS NULL

here [numbers] is table wich conteins numbers
0
1
2
...
10000
 
Back
Top