Pergunta

I have a Drupal site which has a table that keeps track of users. What I want to do is graph membership growth over time. So I want to massage mysql into returning something like this:

date | # of users (total who have registered up to the given date)
1/1/2014 | 0
1/2/2014 | 2
1/3/2014 | 10

Where '# of users' is the total number of users that have registered accounts up to the given date (running-total)--NOT the number of users who registered on that particular day (which is trivial to retrieve).

Each row of my {users} table has a uid column, a name column, and a created (timestamp) column.

So a sample record from my {users} table would be:

name: John Smith
uid: 526
created: 1365844220
Foi útil?

Solução 2

I ended up using a solution that incorporates variables, based on a Stack Overflow answer posted here. This solution appears to be a bit more flexible and efficient than other answers provided.

  SELECT u.date,
  @running_total := @running_total + u.count AS count
  FROM (
    SELECT COUNT(*) AS count, DATE_FORMAT(FROM_UNIXTIME(created), '%b %d %Y') AS date
    FROM {users}
    WHERE created >= :start_time AND created <= :end_time
    GROUP BY YEAR(FROM_UNIXTIME(created)), MONTH(FROM_UNIXTIME(created)), DAY(FROM_UNIXTIME(created))
  ) u
  JOIN (
    SELECT @running_total := u2.starting_total
    FROM (
      SELECT COUNT(*) as starting_total
      FROM {users}
      WHERE created < :start_time
    ) u2
  ) initialize;

Note that the group by, date formatting, and range requirements are simply specifics of my particular project. A more generic form of this solution (as per the original question) would be:

  SELECT u.date,
  @running_total := @running_total + u.count AS count
  FROM (
    SELECT COUNT(*) AS count, DATE(FROM_UNIXTIME(created)) AS date
    FROM {users}
    GROUP BY date
  ) u
  JOIN (
    SELECT @running_total := 0
  ) initialize;

Outras dicas

Try:

select u.created, count(*)
from (select distinct date(created) created from `users`) u
join `users` u2 on u.created >= date(u2.created)
group by u.created

SQLFiddle here.

Don't know the table structure so adjust the query to you needs

SELECT DATE(created), COUNT(*) AS Users FROM users GROUP BY DATE(created)

When you only want to show the dates having registerd users add

HAVING COUNT(*) > 0

At the and of the query

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top