First let me say what I'm working with is not optimal or preferred - (heavy on the sarcasm and lots of )
Also, I have limited table design experience and would great appreciate any help and/or suggestions and I'm fairly new to MySQL. I know there has got to be a way to accomplish this, I'm so close....
Here is the background of what I'm dealing with. (Sorry in advance for the length)
I have two tables - one contains call data and the other contains survey data.
Each has it's own id BUT there is no connection between the two tables except for date/time and there is a timezone difference between the two tables.
In the call table there are separate call time and date fields - both starting and ending and a field identifying that the call was transferred to a survey.
In the survey table there are separate call time and date - both starting and ending
My task is to create a linking table using the call end date/time and the timezone converted survey call start date/time to join on within a 0 to 5 second gap. This is the step is where I run into problems with the one to one unique matching. I have situations like the following where multiple calls can match multiple surveys.
Let's say I have two calls -
Call#1 9/1/2014 10:02:51
Call#2 9/1/2014 10:02:54
That can match two surveys -
Survey#1 9/1/2014 10:02:55
Survey#2 9/1/2014 10:02:56
What I'm running into is that a really small number of the surveys are not being loaded into the CallSurvey table and I think it's because they can match multiple calls but I'm not sure how to redesign it so that if Call#2 shows Survey#1 in the CallSurvey table, when it comes time for Call#1 to update it's Survey_ID, then it can only choose Survey#2. Later you will understand why that is how they should link up based upon the order of the updates.
I created a linking table, it contains the call id, survey id and a flag field to identify calls that connected to the survey - sometimes the call is transferred but the call is hung up or dropped so I need to capture that.
CREATE TABLE IF NOT EXISTS CallSurvey (
Call_ID varchar(254) NOT NULL,
Survey_ID int(11) ,
Completed int(11) NOT NULL Default 0, -- Was originally set as a bit but the tool I'm using for reporting on this data does not handle bit fields well so I reverted back to int(11)
PRIMARY KEY (`Call_ID`),
UNIQUE INDEX (`Survey_ID`)
I drop if exists, then create tables with just the fields I need for both the call and survey data. In the call table I create 5 1 second incremented call end times. I load them with the date and time fields concatenated. In the survey table I convert the timezone to allow them to match and in the case of the call data, load just those calls that have been identified as being transferred. See below:
CREATE TABLE `Call_CONVERTED` (
`Call_ID` varchar(254) NOT NULL,
`Call End Time_CST` datetime DEFAULT NULL,
`Call ET plus 1` datetime DEFAULT NULL,
`Call ET plus 2` datetime DEFAULT NULL,
`Call ET plus 3` datetime DEFAULT NULL,
`Call ET plus 4` datetime DEFAULT NULL,
`Call ET plus 5` datetime DEFAULT NULL,
`Date_Call_Ended` date DEFAULT NULL, -- used for validation
`Time_Call_Ended` time DEFAULT NULL, -- used for validation
`Survey` varchar(15) DEFAULT NULL,
PRIMARY KEY `Call_ID` (`Call_ID`),
KEY `Call End Time_CST` (`Call End Time_CST`),
INDEX `Call ET plus 1` (`Call ET plus 1`),
INDEX `Call ET plus 2` (`Call ET plus 2`),
INDEX `Call ET plus 3` (`Call ET plus 3`),
INDEX `Call ET plus 4` (`Call ET plus 4`),
INDEX `Call ET plus 5` (`Call ET plus 5`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO Call_CONVERTED
(SELECT
Call_ID,
STR_TO_DATE(concat(Date_Call_Ended, ' ', Time_Call_Ended), '%Y-%m-%d %H:%i:%s') AS `Call End Time_CST`,
Date_add(STR_TO_DATE(concat(Date_Call_Ended, ' ', Time_Call_Ended), '%Y-%m-%d %H:%i:%s'), INTERVAL 1 SECOND) as `Call ET plus 1`,
Date_add(STR_TO_DATE(concat(Date_Call_Ended, ' ', Time_Call_Ended), '%Y-%m-%d %H:%i:%s'), INTERVAL 2 SECOND) as `Call ET plus 2`,
Date_add(STR_TO_DATE(concat(Date_Call_Ended, ' ', Time_Call_Ended), '%Y-%m-%d %H:%i:%s'), INTERVAL 3 SECOND) as `Call ET plus 3`,
Date_add(STR_TO_DATE(concat(Date_Call_Ended, ' ', Time_Call_Ended), '%Y-%m-%d %H:%i:%s'), INTERVAL 4 SECOND) as `Call ET plus 4`,
Date_add(STR_TO_DATE(concat(Date_Call_Ended, ' ', Time_Call_Ended), '%Y-%m-%d %H:%i:%s'), INTERVAL 5 SECOND) as `Call ET plus 5`,
Date_Call_Ended,
Time_Call_Ended,
Survey
FROM Calls
WHERE Survey = '1234567890');
CREATE TABLE `Survey_CONVERTED` (
`survey_id` int(11) NOT NULL,
`Call Start Time_Converted` datetime DEFAULT NULL,
`call_date` date DEFAULT NULL,
`call_time_start` time DEFAULT NULL,
PRIMARY KEY `survey_id` (`survey_id`),
KEY `Call Start Time_Converted` (`Call Start Time_Converted`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO Survey_CONVERTED
(SELECT
survey_id,
Date_ADD(STR_TO_DATE(concat(call_date, ' ', call_time_start), '%Y-%m-%d %H:%i:%s'), INTERVAL 1 Hour) AS `Call Start Time_Converted`,
call_date, -- used for validation
call_time_start -- used for validation
FROM Survey);
Next I loaded all the calls into the linking table.
<Each update statement runs separately - I have to do that based on the tool I'm using, so they are in running in order of Call_CONVERTED.Call End Time = Survey_CONVERTED.Call Start Time_Converted Matching exactly, then the one second difference and so on until it completes the update of the 5 second difference.>
Below is an example of an updates using the 3 second difference timeframe.
UPDATE IGNORE CallSurvey
INNER JOIN Call_CONVERTED
ON CallSurvey.Call_ID = Call_CONVERTED.Call_ID
INNER JOIN Survey_CONVERTED
ON Call_CONVERTED.`Call ET plus 3` = Survey_CONVERTED.`Call Start Time_Converted`
SET survey_id = Survey_CONVERTED.survey_id,
Completed = 1;
I tried adding a where clause to limit the available surveys to include in the update but I wasn't sure how to limit them. I'm sure at this point it's something simple but I'm missing it. Any help you can provide would be appreciated. Thanks.
Also, I have limited table design experience and would great appreciate any help and/or suggestions and I'm fairly new to MySQL. I know there has got to be a way to accomplish this, I'm so close....
Here is the background of what I'm dealing with. (Sorry in advance for the length)
I have two tables - one contains call data and the other contains survey data.
Each has it's own id BUT there is no connection between the two tables except for date/time and there is a timezone difference between the two tables.
In the call table there are separate call time and date fields - both starting and ending and a field identifying that the call was transferred to a survey.
In the survey table there are separate call time and date - both starting and ending
My task is to create a linking table using the call end date/time and the timezone converted survey call start date/time to join on within a 0 to 5 second gap. This is the step is where I run into problems with the one to one unique matching. I have situations like the following where multiple calls can match multiple surveys.
Let's say I have two calls -
Call#1 9/1/2014 10:02:51
Call#2 9/1/2014 10:02:54
That can match two surveys -
Survey#1 9/1/2014 10:02:55
Survey#2 9/1/2014 10:02:56
What I'm running into is that a really small number of the surveys are not being loaded into the CallSurvey table and I think it's because they can match multiple calls but I'm not sure how to redesign it so that if Call#2 shows Survey#1 in the CallSurvey table, when it comes time for Call#1 to update it's Survey_ID, then it can only choose Survey#2. Later you will understand why that is how they should link up based upon the order of the updates.
I created a linking table, it contains the call id, survey id and a flag field to identify calls that connected to the survey - sometimes the call is transferred but the call is hung up or dropped so I need to capture that.
CREATE TABLE IF NOT EXISTS CallSurvey (
Call_ID varchar(254) NOT NULL,
Survey_ID int(11) ,
Completed int(11) NOT NULL Default 0, -- Was originally set as a bit but the tool I'm using for reporting on this data does not handle bit fields well so I reverted back to int(11)
PRIMARY KEY (`Call_ID`),
UNIQUE INDEX (`Survey_ID`)
I drop if exists, then create tables with just the fields I need for both the call and survey data. In the call table I create 5 1 second incremented call end times. I load them with the date and time fields concatenated. In the survey table I convert the timezone to allow them to match and in the case of the call data, load just those calls that have been identified as being transferred. See below:
CREATE TABLE `Call_CONVERTED` (
`Call_ID` varchar(254) NOT NULL,
`Call End Time_CST` datetime DEFAULT NULL,
`Call ET plus 1` datetime DEFAULT NULL,
`Call ET plus 2` datetime DEFAULT NULL,
`Call ET plus 3` datetime DEFAULT NULL,
`Call ET plus 4` datetime DEFAULT NULL,
`Call ET plus 5` datetime DEFAULT NULL,
`Date_Call_Ended` date DEFAULT NULL, -- used for validation
`Time_Call_Ended` time DEFAULT NULL, -- used for validation
`Survey` varchar(15) DEFAULT NULL,
PRIMARY KEY `Call_ID` (`Call_ID`),
KEY `Call End Time_CST` (`Call End Time_CST`),
INDEX `Call ET plus 1` (`Call ET plus 1`),
INDEX `Call ET plus 2` (`Call ET plus 2`),
INDEX `Call ET plus 3` (`Call ET plus 3`),
INDEX `Call ET plus 4` (`Call ET plus 4`),
INDEX `Call ET plus 5` (`Call ET plus 5`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO Call_CONVERTED
(SELECT
Call_ID,
STR_TO_DATE(concat(Date_Call_Ended, ' ', Time_Call_Ended), '%Y-%m-%d %H:%i:%s') AS `Call End Time_CST`,
Date_add(STR_TO_DATE(concat(Date_Call_Ended, ' ', Time_Call_Ended), '%Y-%m-%d %H:%i:%s'), INTERVAL 1 SECOND) as `Call ET plus 1`,
Date_add(STR_TO_DATE(concat(Date_Call_Ended, ' ', Time_Call_Ended), '%Y-%m-%d %H:%i:%s'), INTERVAL 2 SECOND) as `Call ET plus 2`,
Date_add(STR_TO_DATE(concat(Date_Call_Ended, ' ', Time_Call_Ended), '%Y-%m-%d %H:%i:%s'), INTERVAL 3 SECOND) as `Call ET plus 3`,
Date_add(STR_TO_DATE(concat(Date_Call_Ended, ' ', Time_Call_Ended), '%Y-%m-%d %H:%i:%s'), INTERVAL 4 SECOND) as `Call ET plus 4`,
Date_add(STR_TO_DATE(concat(Date_Call_Ended, ' ', Time_Call_Ended), '%Y-%m-%d %H:%i:%s'), INTERVAL 5 SECOND) as `Call ET plus 5`,
Date_Call_Ended,
Time_Call_Ended,
Survey
FROM Calls
WHERE Survey = '1234567890');
CREATE TABLE `Survey_CONVERTED` (
`survey_id` int(11) NOT NULL,
`Call Start Time_Converted` datetime DEFAULT NULL,
`call_date` date DEFAULT NULL,
`call_time_start` time DEFAULT NULL,
PRIMARY KEY `survey_id` (`survey_id`),
KEY `Call Start Time_Converted` (`Call Start Time_Converted`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO Survey_CONVERTED
(SELECT
survey_id,
Date_ADD(STR_TO_DATE(concat(call_date, ' ', call_time_start), '%Y-%m-%d %H:%i:%s'), INTERVAL 1 Hour) AS `Call Start Time_Converted`,
call_date, -- used for validation
call_time_start -- used for validation
FROM Survey);
Next I loaded all the calls into the linking table.
<Each update statement runs separately - I have to do that based on the tool I'm using, so they are in running in order of Call_CONVERTED.Call End Time = Survey_CONVERTED.Call Start Time_Converted Matching exactly, then the one second difference and so on until it completes the update of the 5 second difference.>
Below is an example of an updates using the 3 second difference timeframe.
UPDATE IGNORE CallSurvey
INNER JOIN Call_CONVERTED
ON CallSurvey.Call_ID = Call_CONVERTED.Call_ID
INNER JOIN Survey_CONVERTED
ON Call_CONVERTED.`Call ET plus 3` = Survey_CONVERTED.`Call Start Time_Converted`
SET survey_id = Survey_CONVERTED.survey_id,
Completed = 1;
I tried adding a where clause to limit the available surveys to include in the update but I wasn't sure how to limit them. I'm sure at this point it's something simple but I'm missing it. Any help you can provide would be appreciated. Thanks.