开发者

SQL - Getting next number in line from a Recordset with Gaps in Numbers

I have a SQL Record that has number 开发者_如何学编程that are all out of order (has gaps) for example: 1,2,3,7,9,11..... I would like to get only the first missing number.

I tried something like this but I just don't know SQL to make it work correctly.

strQuery = "SELECT N as NextNum FROM LOADS WHERE N > 0 AND N <= (SELECT MAX(LOAD_NO) FROM @LOADS) AND N NOT IN (SELECT LOAD_NO FROM @LOADS)"
objNextNum.Open strQuery
lastLoadNo= objNextNum("NextNum")
response.write "NEXT LOAD NUMBER IS: " & lastLoadNo

I found that example on a ms website but I cannot get it to work.

Thanks for any help!!!


saw this somewhere, can't remember where though, or i'd give proper attribution, but you should be able to use this:

SELECT  TOP 1
        LOAD_NO + 1
FROM    LOADS mo
WHERE   NOT EXISTS
        (
        SELECT  NULL
        FROM    LOADS mi 
        WHERE   mi.LOAD_NO = mo.LOAD_NO + 1
        )
ORDER BY
        id
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜