SELECT CUST_ID, AVG(freq), AVG(amount) ,month from
(SELECT CUST_ID, DATE_FORMAT(CDATE, "%Y%m") as month,
COUNT(*) as freq, SUM(BILLS/count(*)) as amount FROM PROCESSED
where CDATE>= DATE(NOW() - INTERVAL 6 MONTH) GROUP BY CUST_ID, month having count(*) >=3
order by cust_id, month) T where CUST_ID != 2750 and CUST_ID != 1 group by CUST_ID
I understand the group by clause does not allow non-agg column, I need the month as a column?