Find growth from dates group by months This is a follow-up to my previous question Find month to month percentage growth rate

I have a table called Lead. I need to find out the month to month increase in lead how can achieve this in MySQL



I need to find percentage of increase/decrease  in Lead each months..

Table

CREATE TABLE Lead(leadId int(11) NOT NULL AUTO_INCREMENT,
  leadType varchar(99) DEFAULT NULL,
  leadName varchar(99) DEFAULT NULL,
  leadDate date DEFAULT NULL,
  PRIMARY KEY (leadId));


Table Data

insert into Lead values
(16,'individual','dinesh','2016-06-18'),
(15,'corporate','benz','2017-05-10'),
(14,'individual','fino','2017-05-05'),
(13,'individual','jubin','2017-04-30'),
(12,'corporate','chola','2017-06-16'),
(10,'corporate','Volvo','2017-03-31'),
(9,'individual','manoj','2017-06-16'),
(8,'individual','vijay','2017-06-16'),
(6,'corporate','CHS','2017-06-16'),
(5,'corporate','sastra','2017-06-16'),
(4,'individual','rajesh','2017-06-16'),
(3,'individual','karthi','2017-06-16'),
(2,'individual','vijo','2017-06-16'),
(1,'individual','suresh','2017-06-16');
AS I explained on my previous answer you can use a variable to store last entry value.

In this case you need to count entries grouped by year and month.

SELECT Year, Month, Entry, 
       IF(@last_entry = 0, 0, ((entry - @last_entry) / entry) * 100) "Growth Rate",
       @last_entry := entry                  
FROM
      (SELECT @last_entry := 0) x,
      (SELECT YEAR(leadDate) Year, MONTH(leadDate) Month, COUNT(*) Entry
       FROM   Lead
       GROUP BY 1,2) y;


This is the result:

| Year | Month | Entry | Growth Rate      |
|------|-------|-------|------------------|
| 2016 | 6     | 1     | 0                |
| 2017 | 3     | 1     | 0                |
| 2017 | 4     | 1     | 0                |
| 2017 | 5     | 2     | 50               |
| 2017 | 6     | 9     | 77,7777777777778 |


Rextester here