If i was to have a table with an integer column containing n number of rows and wanted to check if they were consecutive, how could I do this?
DECLA
SELECT CASE
WHEN COUNT(DISTINCT IntegerValue) /*Or COUNT(*) dependant on how
duplicates should be treated */
= 1 + MAX(IntegerValue) - MIN(IntegerValue) THEN 'Y'
ELSE 'N'
END
FROM @Temp
If you want to know where the gaps are you can use
;WITH T AS
(
SELECT *,
DENSE_RANK() OVER (ORDER BY IntegerValue) - IntegerValue AS Grp
FROM @Temp
)
SELECT MIN(IntegerValue) AS RangeStart,
MAX(IntegerValue) AS RangeEnd
FROM T
GROUP BY Grp
ORDER BY MIN(IntegerValue)