MySQL: AVG of defined variable per month
===

I'm a complete SQL Newbie and need help with the following code:

    SELECT DISTINCT userid, count( userid ) as login_count
    FROM (SELECT DISTINCT userid, date( FROM_UNIXTIME( date_time ) ) AS DAY
    FROM xcart_login_history
    WHERE status="success" and (action ="login" or action = "autologin") 
    ORDER BY userid, DAY) as login_days
    WHERE login_days.DAY 
    < 
    (SELECT DISTINCT min(date(FROM_UNIXTIME(xcart_orders.date)))
    FROM xcart_orders
    WHERE xcart_orders.userid = login_days.userid
    GROUP BY userid )
    GROUP BY userid

What it does: it shows me the logins of individual users before purchasing (on purpose not more than one per day counted). I now need the average login number / login count before purchase on a monthly basis.

Could please somebody help me? Thank!