aggregate queries optimisation
Ok. I found the solution.

SELECT 
  SUM(IF(day1 &gt;0, 1, 0))
  #...
  FROM(
      SELECT
      payment_account_id, sum(day1) AS day1
      #...
        FROM (
           SELECT
             payment_account_id,
             IF((purchase_date &lt; 1345120085 AND (expires_date &gt; 1345120085 OR expires_date_calculated &gt; 1345120085)), 1, 0) AS day1
            #...
           FROM transaction
           WHERE
             `product_id` IN
             (
               'a',
               'b'
             )
             AND payment_account_id IS NOT NULL
          ) AS tmp
      GROUP BY payment_account_id
     )
     AS tmp1;