How to find “holes” in a table

后端 未结 10 2194
野趣味
野趣味 2020-12-24 04:28

I recently inherited a database on which one of the tables has the primary key composed of encoded values (Part1*1000 + Part2).
I normalized that column, but I cannot ch

10条回答
  •  温柔的废话
    2020-12-24 04:50

    This will give you the complete picture, where 'Bottom' stands for gap start and 'Top' stands for gap end:

        select *
        from 
        ( 
           (select +1 as id, 'Bottom' AS 'Pos' from  /*where 
           except
           select , 'Bottom' AS 'Pos' from  /*where */)
        union
           (select -1 as id, 'Top' AS 'Pos' from  /*where */
           except
           select , 'Top' AS 'Pos' from  /*where */)
        ) t
        order by t.id, t.Pos
    

    Note: First and Last results are WRONG and should not be regarded, but taking them out would make this query a lot more complicated, so this will do for now.

提交回复
热议问题