We are working with a customer, and the max characters they have for a value field is 10. Therefore, if we have a value record that has more characters than 10, say 50000000.00 (which is 11), the record will be rejected from their system. What I'd like to do is take that value that is in one record and split it into multiple records. Take for example, this basic record:
SELECT 'ACCT_A' ACCT_NUM, 50000000.00 TOT_AMT
FROM TABLE_NAME
FETCH FIRST 1 ROWS ONLY;
ACCT_NUM | TOT_AMT |
---|---|
ACCT_A | 50000000.00 |
I want to create a dynamic SQL statement that will continue to split that one record into multiple records that we can then pass to our customer. I was trying to do something with the LAG function, but I couldn't quite seem to get it. Since 9999999.99 is the largest amount that they can have (which is the highest value number where the max character length is 10), I was attempting to come up with a query where the final output would be similar to this:
ACCT_NUM | TOT_AMT |
---|---|
ACCT_A | 9999999.99 |
ACCT_A | 9999999.99 |
ACCT_A | 9999999.99 |
ACCT_A | 9999999.99 |
ACCT_A | 9999999.99 |
ACCT_A | 0.05 |
Can anyone help make this happen? The TOT_AMT won't always be 50,000,000, so the query would need to be dynamic enough to take in any value as the TOT_AMT and split it accordingly, if needed (which would probably include some sort of CASE statement like so: CASE WHEN TOT_AMT > 9999999.99 THEN 9999999.99 ELSE TOT_AMT END).
This was as far as I was able to get:
WITH TEMP_SPLIT_TABLE AS (
SELECT 'ACCT_A' ACCT_NUM, 50000000.00 TOT_AMT FROM TABLE_NAME FETCH FIRST 1 ROWS ONLY
)
SELECT ACCT_NUM, TOT_AMT, CASE WHEN TOT_AMT > 9999999.99 THEN 9999999.99 ELSE TOT_AMT END AS ACCT_AMT,
CASE WHEN TOT_AMT > 9999999.99 THEN LAG(TOT_AMT - 9999999.99) OVER (PARTITION BY ACCT_NUM ORDER BY 1) ELSE 0 END LAG_VAL
FROM TEMP_SPLIT_TABLE
I only got one record returned, and I'm not sure how to tune the SQL where it will continue to run this and use the LAG function until that TOT_AMT value has been split up to something that is =< 9999999.99.