r/DatabaseHelp • • Sep 27 '18

Converting flat table into relational model

I own a small insurance business and an importing my data into a CRM. The CRM Company told me they would import all of my data for me, but despite the fact that i told them the format my data was in they are now insisting it has to be in a specific format, which it is not. Now they are trying to charge me a ridiculous fee to import it that i am refusing to pay them on principle of they misinformed me.

Good news is I used to work with MySQL a lot as i got of got shoe-horned into a quasi-dba position in my old career where i was a linux admin. So i'm familiar with mysql somewhat, however it's been a couple of years and i'm feeling a bit out of my comfort zone and looking for some guidance.

Currently I have a CSV file with all of my client data, 1 row per policy, each policy covers a single individual (no group policies), however, i may have multiple policies per individual, or per household.

For the CRM i need to normalize this data in order to input it. Into tables for Account, Contact, Carrier, Carrier Product, Policy. And return it to them as a CSV.

I have no idea how to convert this flat file into a relational model, and that's what i'm asking for help with. If i have to pay a small fee for a software package to assist i'm not against it. If it's just a bunch of steps in sql i'm not opposed to that either, but i'm looking to get this done as quickly and painlessly as possible.

Here is a simplified example of what i have and what i need:

Currently I have an excel spreadsheet similar to this:

FNameLnameAddressCarrierCarrierProductEffectiveDate
JohnSmith123 California AveAnthemPlusPlan09/01/2018
MariaSmith123 California AveAnthemElitePlan08/01/2015
RichardJohnson84 New Jersey WayAetnaPPOSelect01/01/2010
JohnSmith123 Calofirnia AveStateFarmTermLife02/01/2016

I need to turn this into a relational model that looks something like this:

Carrier Table:

carrier idcarrier
1anthem
2aetna
3StateFarm

Carrier Product Table:

Carrier product idproduct namecarrierReference
1PlusPlan1
2ElitePlan1
3PPOSelect2
4TermLife3

Contact:

ContactIDAccountIDRefFNameLNameAddress
11JohnSmith123 California Ave
21MariaSmith123 California Ave
32RichardJohnson85 New Jersey Way

Account:

AccountIDAccountNamePrimaryContactIDRef
1John & Maria Smith Household1
2Richard Johnson Household3

Policy:

PolicyIDAccountIDRefContactIDRefCarrierIDRefCarrierProductIDRefEffective Date
1111109/01/2018
2121208/01/2015
3232301/01/2010
4113402/01/2016
3 Upvotes

4 comments sorted by

3

u/wolf2600 Sep 28 '18 edited Sep 28 '18

The first step will be to load the data you have into the database as-is. Just create a single table and load the data in there.

Then you'll need to write several SQL scripts to pull out the data to populate your various new tables:

Assuming you insert your existing data into a table: oldData.

Then you'd create a table called Carriers with an auto incrementing ID column and do:

create table Carriers
(carrier_id    AUTO_INCREMENT NUMERIC PRIMARY KEY,   --(or whatever)
 carrier_name    varchar(20));

insert into Carriers
(carrier_name)
(select distinct carrier from oldData);

Then you'd create CarrierProducts and populate it:

create table CarrierProducts
(product_id    AUTO_INCREMENT NUMERIC PRIMARY KEY,
product_name    varchar(20),
carrier_id    REFERENCES carriers(carrier_id));

insert into CarrierProducts
(product_name, carrier_id)
(select distinct od.product_name, c.carrier_id from oldData od
inner join Carriers c
on od.Carrier = c.carrier_name);

And so on until you've populated each table using the data from the original oldData table.

2

u/flying_unicorn Sep 29 '18

Sweet Jesus Thank You! The groundwork you handed me set me on the right path!

It took me all day today, mostly due to some issues with my data, but I just got it done!

3

u/chrwei Sep 27 '18

typically, you write a program to parse the file and create records as needed, doing text based searches to see if records already exist and pulling the ID. Hopefully the data is consistent, nothing like having Rick Smith and Richard Smith but it's the same person.

if the flat file is small enough you might get away with doing it manually using sorting, copying fields to new sheets, running a distinct operation, generating IDs, and making link tables.

it's a lot of work either way, especially if you don't have import/API access yourself and the ability to purge it all and start over.