我有两个表可供使用,USERS,METRICS_DAILY
的MySQL>描述USERS;
+---------------------------+---------------------+------+-----+------------------------------------------+----------------+
| 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中的EVENT是month1,month2,month3,... monthCURRENT。
我几乎完全可以在METRICS_DAILY表中完成它,因为我将DATE与USERS.CREATED对应,只是间接地。
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)EVENT> 10月和DATE = DATE(NOW())按MONTH(创建)分组;
+---------------------------------------+---------------------+----------------+------------+
| 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秒)
答案 0 :(得分: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)
答案 1 :(得分: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;