Migrate existing state history to statistics

I would like to use one of my sensors in a statistics graph, however, it did not have a valid state_class for the last 1+ year, so the state data was stored in the states table instead of the long term statistics table.

Since the data is there, is there a tool I can use (or a SQL script), that converts my data from the states table to the statistics table?

My one sensor here (odometer for my car) has over 400.000 states, so I’m guessing the script needs to do some “group by 1 minute” or similar, so the statistics table (with only 900.000 rows) doesn’t get overflooded.

Any ideas?