我有一个带
的用户表id ... created_at (datetime) country_code ...
并希望进行一个查询,输出我在分组时间段中混淆的国家/地区代码:
Rows = US, UK, MX, ZH, ...
Columns = 2009-01, 2009-02, 2009-03, etc
cells = the count of cumulate e.g. UK signups before 2010-08-01
我已经到了
SELECT country_code, count(created_at)
FROM users
WHERE date(created_at) < "2010-08-01"
GROUP BY country_code
ORDER BY country_code ASC;
但这只能让我获得一次切片的快照。我怎么能让它有很多列
date(created_at) < 2010-08
date(created_at) < 2010-09
date(created_at) < 2010-10
所有在一个查询中?
使用MYSQL
实际数据
用户表:
id | country_code | created_at
1 | US | 2010-03-04 12:34:56
2 | MX | 2010-04-05 07:08:09
3 | DE | 2010-10-12 12:34:56
4 | US | 2011-01-04 12:34:56
5 | DE | 2011-01-04 12:34:56
6 | DE | 2011-01-30 12:34:56
期望的结果
cc | 2010-03 | 2010-04 | 2010-10 | 2011-01 |
US | 1 | 1 | 1 | 2 |
MX | 0 | 1 | 1 | 1 |
DE | 0 | 0 | 1 | 3 |