Mysql:对有序结果进行分组

时间:2014-04-29 16:36:17

标签: mysql group-by sql-order-by

假设我有一个学校表(cols = "ids (int)")和一个用户表(cols = "id (int), school_id (int), created_at (datetime)")

我有<school_ids>中保存的学校ID列表。我想根据最早的created_at值为该学校的用户yearweek(users.created_at)值分组这些学校,并为每个组列出yearweek(users.created_at)的值和学校数。

换句话说,我想为每所学校找到最早创建的用户,然后按照创建日期的yearweek()结果对学校进行分组,这样我就有了第一批注册的学校数量用户每周都有效。

所以,我想要像

这样的结果
| 201301 | 22 |  #meaning there are 22 schools where the earliest created_at user 
                 #has yearweek(created_at) = "201301"

| 201302 | 5  |  #meaning there are 5 schools where the earliest created_at user  
                 #has yearweek(created_at) = "201302"

作为完整性检查,第二列中所有行的总和应等于<school_ids>的大小,即school_ids中的ID数。

这有意义吗?我无法弄清楚如何在不进行多次查询和在两者之间存储值的情况下获得此信息。我敢肯定有一个单行。谢谢!最大

1 个答案:

答案 0 :(得分:1)

您可以使用子查询返回每个school_id的最小created_at字段,然后您可以按周分组并进行计数:

SELECT
  yearweek(u.min_created_at) AS yearweek_first_user,
  COUNT(*)
FROM
  (
    SELECT school_id, MIN(created_at) AS min_created_at
    FROM users
    GROUP BY school_id
  ) u
GROUP BY
  yearweek(u.min_created_at)