SQL 查询 JOIN 和聚合
SQL Query JOIN and Aggregate
SQL 的新手,谷歌搜索无果。这是架构:
"users"
Column | Type |
---------------------+--------------------------+
id | text |
name | text |
title | text |
org_id | text |
type. | text |
"organizations"
Column | Type |
--------------------------------+--------------------------+
id | text |
name | text |
"posts"
Column | Type |
-------------------+--------------------------+
id | text |
title | text |
content | jsonb[] |
owner_id | text |
org_id | text |
is_public | boolean |
我的目标是在一个 table 中显示每个组织有多少私人主题、管理员和标准用户,如下所示:
Org | Users | Admins | Private Posts
----------+-------+-------+---------------
Org1 | 56 | 10 | 22
Org2 | 111 | 10 | 34
现在,我只得到这个:
Org | Count | Type | Private Posts
----------+-------+-------+---------------
Org1 | 10 | admin | 22
Org2 | 111 | user | 34
Org1 | 56 | user | 22
Org2 | 10 | admin | 34
使用:
SELECT t1.id as "Org", t1.cnt as "Count", t1.type as "Type", t2.cnt as "Private Posts" from
(SELECT COUNT(u.type) as "cnt", u.type as "type", o.id FROM "users" AS u JOIN
"organizations" AS o ON o.id=u.org_id GROUP BY u.type, o.id) as t1 join
(SELECT COUNT(org_id) as "cnt", org_id from posts WHERE is_public = False group
by org_id) as t2 on t2.org_id = t1.id;
我基本上尝试加入用户和组织并根据组织和用户类型(t1)进行计数,然后统计帖子(t2)中的 public 个帖子,并尝试根据加入 t1 和 t2组织 ID。感谢任何帮助。
考虑一个名为 条件聚合 的概念,它根据条件逻辑将结果转换为所需的宽格式列。 Postgres 为这种类型的查询维护有用的 FILTER
,但也可以使用与其他 RDBMS 共享的 CASE
语句:
SELECT o.id AS "Org",
COUNT(*) FILTER(WHERE u.type = 'user') AS "Users",
COUNT(*) FILTER(WHERE u.type = 'admin') AS "Admins",
COUNT(*) FILTER(WHERE is_public = False) AS "Private Posts"
FROM users u
JOIN organizations o
ON o.id = u.org_id
JOIN posts p
ON o.id = p.org_id
GROUP BY o.id;
SQL 的新手,谷歌搜索无果。这是架构:
"users"
Column | Type |
---------------------+--------------------------+
id | text |
name | text |
title | text |
org_id | text |
type. | text |
"organizations"
Column | Type |
--------------------------------+--------------------------+
id | text |
name | text |
"posts"
Column | Type |
-------------------+--------------------------+
id | text |
title | text |
content | jsonb[] |
owner_id | text |
org_id | text |
is_public | boolean |
我的目标是在一个 table 中显示每个组织有多少私人主题、管理员和标准用户,如下所示:
Org | Users | Admins | Private Posts
----------+-------+-------+---------------
Org1 | 56 | 10 | 22
Org2 | 111 | 10 | 34
现在,我只得到这个:
Org | Count | Type | Private Posts
----------+-------+-------+---------------
Org1 | 10 | admin | 22
Org2 | 111 | user | 34
Org1 | 56 | user | 22
Org2 | 10 | admin | 34
使用:
SELECT t1.id as "Org", t1.cnt as "Count", t1.type as "Type", t2.cnt as "Private Posts" from
(SELECT COUNT(u.type) as "cnt", u.type as "type", o.id FROM "users" AS u JOIN
"organizations" AS o ON o.id=u.org_id GROUP BY u.type, o.id) as t1 join
(SELECT COUNT(org_id) as "cnt", org_id from posts WHERE is_public = False group
by org_id) as t2 on t2.org_id = t1.id;
我基本上尝试加入用户和组织并根据组织和用户类型(t1)进行计数,然后统计帖子(t2)中的 public 个帖子,并尝试根据加入 t1 和 t2组织 ID。感谢任何帮助。
考虑一个名为 条件聚合 的概念,它根据条件逻辑将结果转换为所需的宽格式列。 Postgres 为这种类型的查询维护有用的 FILTER
,但也可以使用与其他 RDBMS 共享的 CASE
语句:
SELECT o.id AS "Org",
COUNT(*) FILTER(WHERE u.type = 'user') AS "Users",
COUNT(*) FILTER(WHERE u.type = 'admin') AS "Admins",
COUNT(*) FILTER(WHERE is_public = False) AS "Private Posts"
FROM users u
JOIN organizations o
ON o.id = u.org_id
JOIN posts p
ON o.id = p.org_id
GROUP BY o.id;