Find clients who bought more than 1 million units in 3 consecutive months
Before the edit

I created a table to test here the create code ( you will have to alter your table and add unique identifier for this to work )

CREATE TABLE `milad` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `cCode` int(10) unsigned NOT NULL,
 `gDate` date NOT NULL,
 `buy` int(10) unsigned NOT NULL,
 PRIMARY KEY (`id`)
)


Then I inserted the date you used and test with 3 days not 3 months

SELECT t2.cCode,sum(t2.buy) AS total 
FROM milad As t1 
INNER JOIN milad AS t2 
ON t1.cCode=t2.cCode 
WHERE t1.gDate&lt;=DATE_ADD(t2.gDate,INTERVAL 3 day) 
group by (t1.id) 
having total&gt;1000000 
ORDER BY t1.cCode , total DESC


Here is the result

+----------+---------+
| cCode    | total   |
+----------+---------+
| 12400573 | 4008000 |
| 12400573 | 2672000 |
| 12400573 | 1336000 |
| 12600125 | 4859000 |
| 12600125 | 3592800 |
| 15108164 | 3306820 |
| 15108164 | 1653410 |
| 16100286 | 4251290 |
| 16100286 | 2995570 |
| 16100286 | 1255720 |
+----------+---------+




After the edit

SELECT t1.cCode
FROM milad As t1
INNER JOIN milad AS t2
ON t1.cCode=t2.cCode
and (
  (
    year(t2.gDate) = year(DATE_ADD(t1.gDate,INTERVAL 1 month)) 
    and month(t2.gDate) = month(DATE_ADD(t1.gDate,INTERVAL 1 month))
  )
  or 
  (
    year(t2.gDate) = year(DATE_ADD(t1.gDate,INTERVAL 2 month)) 
    and month(t2.gDate) = month(DATE_ADD(t1.gDate,INTERVAL 2 month))
  )
)
group by t1.cCode,t1.gDate
having count(t1.gDate)=2


The result

+----------+
| cCode    |
+----------+
| 16100286 |
+----------+




Another solution using a function

Function code

DELIMITER $$
CREATE FUNCTION milad(D date,I int) RETURNS boolean
BEGIN
if
(SELECT count(cCode) as a FROM milad WHERE
    year(gDate) = year(DATE_ADD(D,INTERVAL 1 month))
    and month(gDate) = month(DATE_ADD(D,INTERVAL 1 month))
    and buy&gt;1000000
    and cCode=I
 )&gt;0
and 
(SELECT count(cCode) as a FROM milad WHERE
    year(gDate) = year(DATE_ADD(D,INTERVAL 2 month))
    and month(gDate) = month(DATE_ADD(D,INTERVAL 2 month))
    and buy&gt;1000000
    and cCode=I
 )&gt;0
THEN RETURN true;
ELSE RETURN false;
end if;
end$$
DELIMITER ;


Then just use this query

SELECT cCode
FROM milad
where milad(gDate, cCode)


The result

+----------+
| cCode    |
+----------+
| 16100286 |
+----------+


This will work if there are two rows in same month for the same person.

Please if someone can do it without a function, write the answer and mention me.