Mysql
 SQL >> Database >  >> RDS >> Mysql

Totale cumulativo mensile in MySQL

MySQL non ha funzioni analitiche complete come Oracle o SQL Server. Un possibile approccio usa una variabile utente:

SELECT t.Date, t.NewUsers, @rt := @rt + t.NewUsers AS `Running Total`
FROM (SELECT @rt := 0) i
JOIN (
  SELECT DATE_FORMAT(created,'%Y-%m') AS `Date`,
         COUNT(item_id) AS `NewUsers`
  FROM AP_user
  WHERE YEAR(created) > 2011
  GROUP BY DATE_FORMAT(created,'%Y-%m')
  ORDER BY `Date`
) t

Il comportamento delle variabili utente non è garantito in tutti i contesti e può cambiare nelle versioni future.