Singlestore SQL - Split string on column and insert new rows

35 Views Asked by At

I have a sql table which has a string column serial_numbers which is comma separated. I want to split this string on the comma and generate records based on how many serial_numbers were inside and insert into a new table. Below is a sample data set. I want to run this script for every record in the table.

{
        "ticket_id" : 1,
        "item_id" : 2,
        "service_order_id" : 3,
        "serial_numbers" : "W1-21,W1-22,W1-23,W1-24,W1-25",
        "received_qty" : 5,
        "delivery_ticket_number" : "123",
        "received_by" : "BOB",
        "delivery_date" : "2023-12-18 17:11:35",
    }

For the above I would like to generate 5 records each with a unique serial_number from the list. All other attributes would remain the same with the exception of ticket_id as it is the primary auto generated key and received_qty would be 1.

I've tried a few scripts but they all fail on singlestore as many functions are not supported. Below is a script that would not work on singlestore.

INSERT INTO your_table_name (
    item_id,
    service_order_id,
    serial_numbers,
    received_qty,
    delivery_ticket_number,
    received_by,
    delivery_date,
    ticket_uuid
)
SELECT
    item_id,
    service_order_id,
    TRIM(SPLIT(serial_numbers, ',')) AS serial_numbers,
    received_qty,
    delivery_ticket_number,
    received_by,
    delivery_date,
    ticket_uuid
FROM
    your_original_table_name,
    UNNEST(SPLIT(serial_numbers, ',')) AS serial_numbers;
0

There are 0 best solutions below