I have a table that has about 60 million rows that I use for testing. I want to add a column test_time
that should be unique across all rows and spaced 1 second apart from each other.
So for instance:
Row 1: 2024-01-01 00:00:00
Row 2: 2024-01-01 00:00:01
Row 3: 2024-01-01 00:00:02
Row 4: 2024-01-01 00:00:03
...
Is there an easy way to accomplish this through SQL or do I need to write a script to connect to the db and manually do this?
2
Answers
You can use generate sequence function,
For your requirement, use row_number with your actual table and join it with the above query and generate the date with second difference
You can achieve this using SQL directly, utilizing the
ROW_NUMBER()
function along with a timestamp generation mechanism. The idea is to use theROW_NUMBER()
to generate incremental numbers and then add these numbers to a base timestamp to get your desired result. Here’s an example query:This query adds a new column test_time to your existing table and updates it with timestamps incrementing by 1 second for each row. Adjust the table and column names according to your database schema.