Importing csv data into mysql combining several rows of column data into multiple fields within one record

41 Views Asked by At

I am being supplied a CSV/XLSX file exported from a system for which I have no control. The exported CSV file can contain multiple (up to 6) rows for each patient with their different diagnosed conditions on separate rows. I am trying to import all of this data into mysql where each patient (uniquely identified by their medical record number) has all of their diagnosed conditions occupying one of six fields (condition1, condition2, ..., condition6). Is there a clever way to do this on import or do I need to import it "as-is" and then run one or more queries on the resultant table to create a new table the way I want it?

To clarify further, I have an Excel spreadsheet containing thousands of records formatted similar to:

| practice | provider  |   mrn   | patient  | condition  | last_visit |
| -------- | --------- | ------- | -------- | ---------- | ---------- |
| clinic1  | provider1 | 1234567 | patient1 | diagnosis1 | 2022-11-16 |
| clinic1  | provider1 | 1234567 | patient1 | diagnosis2 | 2022-11-16 |
| clinic1  | provider1 | 1234567 | patient1 | diagnosis3 | 2022-11-16 |
| clinic1  | provider1 | 9876543 | patient2 | diagnosis1 | 2023-07-05 |
| clinic1  | provider1 | 9876543 | patient2 | diagnosis2 | 2023-07-05 |
| clinic1  | provider1 | 4561237 | patient3 | diagnosis1 | 2023-07-31 |
| clinic1  | provider1 | 4561237 | patient3 | diagnosis2 | 2023-07-31 |
| clinic1  | provider1 | 4561237 | patient3 | diagnosis3 | 2023-07-31 |
| clinic1  | provider1 | 4561237 | patient3 | diagnosis4 | 2023-07-31 |

I am trying to import the Excel data shown above (as a CSV file) into a mysql database where each record is structured like this:

| practice | provider  |   mrn   | patient  | condition1 | condition2 | condition3 | condition4 | condition5 | condition6 | last_visit |
| -------- | --------- | ------- | -------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- |
| clinic1  | provider1 | 1234567 | patient1 | diagnosis1 | diagnosis2 | diagnosis3 |            |            |            | 2022-11-16 |
| clinic1  | provider1 | 9876543 | patient2 | diagnosis1 | diagnosis2 |            |            |            |            | 2023-07-05 |
| clinic1  | provider1 | 4561237 | patient3 | diagnosis1 | diagnosis2 | diagnosis3 | diagnosis4 |            |            | 2023-07-31 |
0

There are 0 best solutions below