Search code examples
sqlsql-insertrecursive-querymonetdb

how to update missing records in sequence


i have missing records in a sequence and my current output looks like this

|       1882 |   25548860 |         4 | 30                  | null      |       null |
|       1882 |   25548861 |         4 | 30                  | null      |       null |
|       1882 |   25548882 |         4 | 30                  | null      |       null |
|       1882 |   25548883 |         4 | 30                  | null      |       null |
|       1882 |   25548884 |         4 | 30                  | null      |       null |
|       1882 |   25548885 |         4 | 30                  | null      |       null |
                         missing records in between until 2122
|       2122 |   25548860 |         4 | 30                  | null      |       null |
|       2122 |   25548861 |         4 | 30                  | null      |       null |
|       2122 |   25548882 |         4 | 30                  | null      |       null |
|       2122 |   25548883 |         4 | 30                  | null      |       null |
|       2122 |   25548884 |         4 | 30                  | null      |       null |
|       2122 |   25548885 |         4 | 30                  | null      |       null |

I want my output to be in below format. Suggest me a sql query that will update the records in monetdb between 1883 to 2121.

|       1882 |   25548860 |         4 | 30                  | null      |       null |
|       1882 |   25548861 |         4 | 30                  | null      |       null |
|       1882 |   25548882 |         4 | 30                  | null      |       null |
|       1882 |   25548883 |         4 | 30                  | null      |       null |
|       1882 |   25548884 |         4 | 30                  | null      |       null |
|       1882 |   25548885 |         4 | 30                  | null      |       null |

|       1883 |   25548860 |         4 | 30                  | null      |       null |
|       1883 |   25548861 |         4 | 30                  | null      |       null |
|       1883 |   25548882 |         4 | 30                  | null      |       null |
|       1883 |   25548883 |         4 | 30                  | null      |       null |
|       1883 |   25548884 |         4 | 30                  | null      |       null |
|       1883 |   25548885 |         4 | 30                  | null      |       null |
     ........   ..........
     ........   ..........
|       2122 |   25548860 |         4 | 30                  | null      |       null |
|       2122 |   25548861 |         4 | 30                  | null      |       null |
|       2122 |   25548882 |         4 | 30                  | null      |       null |
|       2122 |   25548883 |         4 | 30                  | null      |       null |
|       2122 |   25548884 |         4 | 30                  | null      |       null |
|       2122 |   25548885 |         4 | 30                  | null      |       null |

Solution

  • If you know in advance the range of missing ids, you can use generate_series(). Assuming that your table is called mytable and has columns (id, col1, col2, col3, col4, col5), you can duplicate the records that have id 1882 to fill the gap with the following query:

    insert into mytable (id, co11, col2, col3, col4, col5)
    select value, col1, col2, col3, col4, col5
    from sys.generate_series(1883, 2121, 1)
    cross join mytable t
    where t.id = 1882