0

我有两个表可以使用,USERS,METRICS_DAILY

mysql> 描述用户;

+---------------------------+---------------------+------+-----+------------------------------------------+----------------+
    | Field                     | Type                | Null | Key | Default                                  | Extra          |
    +---------------------------+---------------------+------+-----+------------------------------------------+----------------+
    | USER_ID                   | int(11) unsigned    | NO   | PRI | NULL                                     | auto_increment |
    | CREATED                   | timestamp           | NO   |     | CURRENT_TIMESTAMP|
+---------------------------+---------------------+------+-----+------------------------------------------+----------------+

mysql> 描述 METRICS_DAILY;

+---------+----------------------+------+-----+---------+-------+
| Field   | Type                 | Null | Key | Default | Extra |
+---------+----------------------+------+-----+---------+-------+
| USER_ID | int(11) unsigned     | NO   | PRI | NULL    |       |
| EVENT   | tinyint(3) unsigned  | NO   | PRI | NULL    |       |
| DATE    | date                 | NO   | PRI | NULL    |       |
| COUNT   | smallint(5) unsigned | NO   |     | 0       |       |
+---------+----------------------+------+-----+---------+-------+

我需要选择计数不同的 USER_ID,其中 DATE=DATE(NOW()) 和 (x,y,z) 中的事件和 USERS.CREATED 是月 1、月 2、月 3、...月当前。

我几乎可以完全在 METRICS_DAILY 表中完成它,因为我的 DATE 与 USERS.CREATED 相对应,只是间接地。

Created 是一个完整的时间戳。我需要按月将其分解为同类群组。

这是一个开始:

SELECT count(distinct METRICS_DAILY.USER_ID),CREATED,MONTH(CREATED),DATE FROM METRICS_DAILY LEFT JOIN USERS ON (METRICS_DAILY.USER_ID = USERS.USER_ID) where EVENT > 10 and DATE=DATE(NOW()) group by MONTH(CREATED );

+---------------------------------------+---------------------+----------------+------------+
| count(distinct METRICS_DAILY.USER_ID) | CREATED             | MONTH(CREATED) | DATE       |
+---------------------------------------+---------------------+----------------+------------+
|                                     3 | 2013-01-14 09:35:16 |              1 | 2013-01-18 |
|                                    16 | 2012-12-15 07:07:49 |             12 | 2013-01-18 |
+---------------------------------------+---------------------+----------------+------------+

2 行(0.00 秒)

4

2 回答 2

1

像这样的东西?

SELECT MONTHNAME(u.CREATED), YEAR(u.CREATED), COUNT(*) AS UserCount
FROM METRICS_DAILY md
JOIN USERS u
ON u.USER_ID = md.USER_ID
WHERE md.DATE = NOW() AND md.EVENT in (x,y,z)
GROUP BY MONTHNAME(u.CREATED), YEAR(u.CREATED)
于 2013-01-18T16:25:58.697 回答
0
SELECT DATE(`METRICS_DAILY`.`DATE`) AS `Day of DATE`, `METRICS_DAILY`.`EVENT` AS `EVENT`, COUNT(DISTINCT `METRICS_DAILY`.`USER_ID`) AS `Number of unique User Id`
  FROM METRICS_DAILY
  JOIN USERS ON `METRICS_DAILY`.`USER_ID` = `USERS`.`USER_ID`
  WHERE (`METRICS_DAILY`.`EVENT` >= "31") AND (`METRICS_DAILY`.`DATE` BETWEEN "2012-12-01" AND "2012-12-31") AND (`USERS`.`CREATED` BETWEEN CAST("2012-12-01" AS DATETIME) AND CAST(CONCAT("2012-12-31", " 23:59:59") AS DATETIME))
  GROUP BY DATE(`METRICS_DAILY`.`DATE`), `METRICS_DAILY`.`EVENT`
  ORDER BY `Day of DATE` ASC, `EVENT` ASC;
于 2013-01-25T01:20:04.000 回答