查找有0个帖子,1-5个帖子和> 5个帖子的用户的平均年龄

时间:2012-11-02 16:12:38

标签: mysql sql database database-design aggregate-functions

我需要根据他们的居住国家/地区生成一份关于他们的用户年龄的报告。

以下是我的架构的简化版本:

DESCRIBE countries;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(45) | NO   |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+

DESCRIBE users;
+------------+---------+------+-----+---------+----------------+
| Field      | Type    | Null | Key | Default | Extra          |
+------------+---------+------+-----+---------+----------------+
| id         | int(11) | NO   | PRI | NULL    | auto_increment |
| age        | int(11) | NO   |     | NULL    |                |
| country_id | int(11) | NO   | MUL | NULL    |                |
+------------+---------+------+-----+---------+----------------+

DESCRIBE posts;
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int(11)     | NO   | PRI | NULL    | auto_increment |
| content | text        | NO   |     | NULL    |                |
| user_id | int(11)     | NO   | MUL | NULL    |                |
+---------+-------------+------+-----+---------+----------------+

我想要一个看起来像这样的结果集:

country.name | AVG(age of users with 0 posts) | AVG(age of users with 1-5 posts | AVG(age of users with 1-5 posts
----------------------------------------
Denmark  | 17.4   | 23.2   | NULL
Germany  | 20.1   | 27.8   | 34.7
England  | 31.1   | NULL   | 28.3

其中NULL表示在特定的计数后级别没有用户的国家/地区。例如,丹麦的每个人都有0到5个帖子,而不是更多。我想它不必说NULL,但应该清楚这个值是不确定的。

到目前为止,这是我的查询:

SELECT
    c.name,
    AVG(something) AS avg_age_with_no_posts,
    AVG(something) AS `avg_age_with_1-5_posts`,
    AVG(something) AS `avg_age_with_gt5_posts`
FROM
    users u
    JOIN posts p ON p.user_id=u.id
    JOIN countries c ON c.id=u.country_id
GROUP BY c.id;

我知道它并不多,但我实际上已经挫败了其他条款(嵌套选择,HAVING,COUNT(CASE ...... WHEN ......))相当多(几个小时)。上面的查询只是我知道我需要的基本内容。

谢谢!

1 个答案:

答案 0 :(得分:1)

尝试

 Select c.name,
    Avg(Case When pc.postCount == 0 Then pc.Age End) avgAgeNoPosts,
    Avg(Case When pc.postCount Between 1 And 5 Then pc.Age End) avgAge1_5Posts,
    Avg(Case When pc.postCount > 5 Age End) Then pc.Age End) avgAgeGT5Posts
 From users u
    Join countries c On c.id=u.country_id
    Join (Select user_id uid, Count(*) postCount
          From posts
          Group By user_id ) pc
       On pc.UId = u.id
 Group By c.name

要解释为什么Sum(Case When ... End)表达式在没有else的情况下工作,当When子句中指定的选项都不为true时,将输出null。并且所有聚合运算符(包括Sum())都将忽略空值。