Skip to content

Dividing a sum value into multiple rows due to field length constraint

I am migrating financial data from a very large table (100 million+ of rows) by summarizing the amount and insert them into summary table. I ran into problem when the summary amount (3 billions) is larger than what the field in the summary table can hold (can only hold up to 999 millions.) Changing the field size is not an option as it requires a change process.

The only option I have is to divide the amount (the one that breach the size limit) into smaller ones so it can be inserted into the table.

I came across this SQL – I need to divide a total value into multiple rows in another table which is similar except the number of rows I need to insert is dynamic.

For simplicity, this is how the source table might look like

account_table

The summary records are as follows

Account Summary

Assuming the maximum value in the destination table is 1000.00, the expected output will be

summary_table

How do I create a query to get the expected result? Thanks in advance.

Advertisement

Answer

You need a numbers table. If you have a handful of values, you can define it manually. Otherwise, you might have one on hand or use a similar logic:

User contributions licensed under: CC BY-SA
1 People found this is helpful