MySQL COUNT和GROUP BY子查询

时间:2018-07-24 18:33:07

标签: mysql sql

我正在尝试查找按分支机构和类别分组的帖子数。我没有得到计数为0的类别。

CREATE TABLE branches
    (`id` serial primary key, `name` varchar(7) unique)
;

INSERT INTO branches
    (`id`, `name`)
VALUES
    (1, 'branch1'),
    (2, 'branch2'),
    (3, 'branch3')
;


CREATE TABLE categories
    (`id` serial primary key, `category` varchar(4) unique)
;

INSERT INTO categories
    (`id`, `category`)
VALUES
    (1, 'cat1'),
    (2, 'cat2')
;


CREATE TABLE posts
    (`id` serial primary key, `branch_id` int, `category_id` int, `title` varchar(6), `created_at` varchar(10))
;

INSERT INTO posts
    (`id`, `branch_id`, `category_id`, `title`, `created_at`)
VALUES
    (1, 1, 1, 'Title1', '2017-12-14'),
    (2, 1, 2, 'Title2', '2018-01-05'),
    (3, 2, 1, 'Title3', '2018-01-10')
;

预期输出:

+---------+----------+----+----+
| branch  | category | c1 | c2 |
+---------+----------+----+----+
| branch1 | cat1     |  1 |  0 |
| branch1 | cat2     |  0 |  1 |
| branch2 | cat1     |  0 |  1 |
| branch2 | cat2     |  0 |  0 |
+---------+----------+----+----+

尝试查询:

SELECT b.name, x.c1, y.c2 FROM branches b
LEFT JOIN (
     SELECT COUNT(id) c1 FROM posts WHERE created_at < '2018-01-01'
     GROUP BY posts.branch_id, posts.category_id
) x x.branch_id = b.id
LEFT JOIN (
     SELECT COUNT(id) c2 FROM posts WHERE created_at BETWEEN '2018-01-01' AND '2018-01-31'
     GROUP BY posts.branch_id, posts.category_id
) y y.branch_id = b.id
GROUP BY b.id

3 个答案:

答案 0 :(得分:0)

这似乎可以满足您的要求。

说明:获取帖子中存在的分支的分支/类别的每种可能组合。进行条件求和,以按日期范围和分支/类别获取计数。然后加入分支。

SELECT b.b_id branch,
       b.category,
       COALESCE(Range_Sum.C1,0) C1,
       COALESCE(Range_Sum.C2,0) C2
  FROM ( SELECT b.id b_id,
                c.id c_id,
                c.category
           FROM branches b,
                categories c
          WHERE EXISTS
                  ( SELECT 1
                      FROM posts
                     WHERE b.id = posts.branch_id
                  )
       ) b
  LEFT  
  JOIN (SELECT p.branch_id,
               c.id c_id,
               c.category,
               SUM
                 ( CASE WHEN p.created_at < '2018-01-01' THEN 1 
                        ELSE 0
                    END
                 ) C1,
               SUM
                 ( CASE WHEN p.created_at BETWEEN '2018-01-01' AND '2018-01-31' THEN 1
                        ELSE 0
                    END
                 ) C2
          FROM posts p
         INNER
          JOIN categories c
            ON p.category_id = c.id
         GROUP
            BY p.branch_id,
               c.category,
               c.id
       ) Range_Sum
    ON b.b_id = Range_Sum.branch_id
   AND b.c_id = Range_Sum.c_id;

此外,这只是用于编写易于阅读的查询的内容-切勿使用x和y作为别名。选择其他可能更有用的信息。

答案 1 :(得分:0)

您需要先交叉加入branchescategories;然后左键加入posts并根据您的WHERE条件进行条件计数。

通用格式:

SELECT x.data, y.data
, COUNT(CASE WHEN conditionN THEN 1 ELSE NULL END) AS cN
FROM x CROSS JOIN y
LEFT JOIN z ON x.id = z.x_id AND y.id = z.y_id
GROUP BY x.data, y.data
;

注意:COUNT个(几乎所有聚合函数)忽略NULL值。

答案 2 :(得分:0)

也许有些人为...

SELECT DISTINCT x.branch_id
              , y.category_id
              , COALESCE(z.created_at < '2018-01-01',0) c1
              , COALESCE(z.created_at BETWEEN '2018-01-01' AND '2018-01-31',0) c2
           FROM posts x
           JOIN posts y
           LEFT
           JOIN posts z
             ON z.branch_id = x.branch_id
            AND z.category_id  = y.category_id;

http://sqlfiddle.com/#!9/8aabf2/31