Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL – Split one record into two based on a single field

I have a single record with many fields one of the fields is unsubscribe_type. When the value of unsubscribe_type is ‘Marketing and Reminder’ I want to then create two rows for that record so basically two identical rows except now in unsubscribe_type one row will have ‘Marketing’ the other will have ‘Reminder’

I have tried using a union all and two select queries but the main issue is running it as a CASE/IF statement that Im really struggling with.

Any help is useful not much code to look at currently as Ive tried various ways and no luck so far.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Example of Im looking to do.

Record in a table like this.

    No | Name | Address | unsubscribe_type 
    1 | Andrew | 123 St | Marketing and Reminder

Then I want to query that table and write out to another table two records like this:

    No | Name | Address | unsubscribe_type 
    1 | Andrew | 123 St | Marketing
    2 | Andrew | 123 St | Reminder

>Solution :

Try this

Select no, name, address, itm
From your table, unnest( split (unsubscribe_type, "and")) as itm
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading