With sql find next available integer within range that is not present in existing integer subset(s)

后端 未结 4 1144
谎友^
谎友^ 2021-02-07 12:02

Problem statement:

given a range x -> y of unsigned integers
where x and y are both in the range

4条回答
  •  甜味超标
    2021-02-07 12:34

    I am a little unclear on what your data really looks like. The problem statement, although well-formulated, seems to have little relationship with the query.

    Let me assume that dhcp_range has the data. The query that you want is:

    SELECT COALESCE(MIN(dr.end_address) + 1, 0)
    FROM dhcp_range dr
    WHERE NOT EXISTS (SELECT 1
                      FROM dhcp_range dr2
                      WHERE dr.end_address + 1 BETWEEN dr.start_address AND dr.end_address
                     );
    

提交回复
热议问题