Here is the dummy data , this is the call record data table.
This is a glimpse:
| call_id | customer | company | call_start | |-----------|--------------|-------------|---------------------| |1411482360 | 001143792042 | 08444599175 | 2014-07-31 13:55:03 | |1476992122 | 001143792042 | 08441713191 | 2014-07-31 14:05:10 |
The customer and company fields represent their phone numbers.
- The requirement to calculate the total 'gain' and total "lost" values โโbased on the following logic:
EDIT:
-Customer A calls on company A.
-If customer A calls company B, then company B will receive a +1 win, and company A will lose +1.
-If customer A calls company C, then company C will receive a +1 win, and company B will lose +1.
-If customer A calls company C again, then this will not affect the spill / growth.
- win / loss only enters the game as soon as the 2nd call was made by client A.
- If the customer calls the companies in this order: A, B, B, C, A, A, C, B, D, the process should be as follows:
A -> B -> B +1 gain, A +1 lost B -> C -> C +1 gain, B +1 lost A -> A +1 gain, C +1 lost A -> C -> C +1 gain, A +1 lost B -> B +1 gain, C +1 lost D -> D +1 gain, B +1 lost
After the above process, we should have common values โโlike:
Company Total gain Total lost A 1 2 B 2 2 C 2 2 D 1 0
I started working on this, but itโs wrong, itโs just an idea, it does not give me a separate gain gain and lost values โโbased on the above conditions:
DROP TABLE IF EXISTS GetTotalGainAndLost; CREATE TEMPORARY TABLE IF NOT EXISTS GetTotalGainAndLost AS ( SELECT SUM(count) as 'TotalGainAndLost', `date`, DAY(`date`) as 'DAY' FROM (SELECT count(*) as 'count', customer, `date` FROM (SELECT customer, company, count(*) AS 'count', DATE_FORMAT(`call_end`,'%Y-%m-%d') as 'date' FROM calls WHERE `call_end` LIKE CONCAT(2014, '-', RIGHT(CAST(concat('0', 01) AS CHAR),2),'-%') GROUP BY customer, company, DAY(`call_end`) ORDER BY `call_end` ASC) as tbl1 group by customer, `date` having count(*) > 1) as tbl2 GROUP by `date` ); Select * from GetTotalGainAndLost; DROP TABLE GetTotalGainAndLost;
This query does not show any results.
- The desired result will look like this:
There should be one line per company and date (total winnings and lost calls per day, for example, in January)
| company | totalGain | totalLost | date | DAY | |-------------|------------|-------------|--------------|-------| | 08444599175 | 17 | 6 | 2014-07-01 | 1 | | 08444599175 | 12 | 10 | 2014-07-02 | 2 | | 08444599175 | 3 | 6 | 2014-07-02 | 3 | | 08444599175 | .... | ... | ... | ... | | 08444599175 | 7 | 6 | 2014-07-31 | 31 |