Postgres 交叉表查询以计算每个城市的状态
Postgres Crosstab Query To Count Statuses Per City
我尝试在 Postgres 中 运行 一个需要交叉表的 sql 查询。我以前从未使用过交叉表查询。我的3个table如下图:
位置TABLE:
location_id、lang_id、路径
crm_statuses TABLE:
crm_status_id、crm_status_name
store_crm TABLE:
store_crm_id、status(在 crm_statuses table 的 crm_status_id 中引用),location_id(在 location_id 中引用的位置 table)
我想通过加入 store_crm table 从位置 table 获取位置作为列,或者至少将它们写成硬编码,因为它们只有 3 个(伦敦,曼彻斯特,利兹)。作为行,我想获得 crm 状态。作为内容,我想计算每个位置有多少活跃、不活跃和待定商店。活跃的、不活跃的、待定的是我的crm_statuses。所需的结果 table 将具有以下格式。
Status London Manchester Leeds
Active 2 4 5
Inactive 6 1 3
Pending 4 4 5
我怎样才能做到这一点?
您可以将城市硬编码为以下查询中的列:
SELECT
*
FROM crosstab('
SELECT
cs.crm_status_name,
l.path,
COUNT(*)
FROM store_crm AS sc
JOIN locations AS l ON (sc.location_id=l.location_id)
JOIN crm_statuses AS cs ON (cs.crm_status_id=sc.status)
GROUP BY cs.crm_status_name, l.path
ORDER BY 1,2
',
'
SELECT
path
FROM
locations
') AS f("Status" text, "London" text, "Manchester" text, "Leeds" text);
结果:
Status | London | Manchester | Leeds
----------+--------+------------+-------
Active | 1 | 2 | 1
Inactive | 2 | 1 | 4
Pending | | 1 | 1
(3 rows)
如果您有这样的数据库架构:
test=# \d+ locations
Table "public.locations"
Column | Type | Modifiers | Storage | Stats target | Description
-------------+---------+-----------------------------------------------------------------+----------+--------------+-------------
location_id | integer | not null default nextval('locations_location_id_seq'::regclass) | plain | |
lang_id | integer | | plain | |
path | text | | extended | |
Indexes:
"locations_pkey" PRIMARY KEY, btree (location_id)
Referenced by:
TABLE "store_crm" CONSTRAINT "store_crm_location_id_fkey" FOREIGN KEY (location_id) REFERENCES locations(location_id)
test=# \d+ crm_statuses
Table "public.crm_statuses"
Column | Type | Modifiers | Storage | Stats target | Description
-----------------+---------+----------------------------------------------------------------------+----------+--------------+-------------
crm_status_id | integer | not null default nextval('crm_statuses_crm_status_id_seq'::regclass) | plain | |
crm_status_name | text | | extended | |
Indexes:
"crm_statuses_pkey" PRIMARY KEY, btree (crm_status_id)
Referenced by:
TABLE "store_crm" CONSTRAINT "store_crm_status_fkey" FOREIGN KEY (status) REFERENCES crm_statuses(crm_status_id)
test=# \d+ store_crm
Table "public.store_crm"
Column | Type | Modifiers | Storage | Stats target | Description
--------------+---------+------------------------------------------------------------------+---------+--------------+-------------
store_crm_id | integer | not null default nextval('store_crm_store_crm_id_seq'::regclass) | plain | |
status | integer | | plain | |
location_id | integer | | plain | |
Indexes:
"store_crm_pkey" PRIMARY KEY, btree (store_crm_id)
Foreign-key constraints:
"store_crm_location_id_fkey" FOREIGN KEY (location_id) REFERENCES locations(location_id)
"store_crm_status_fkey" FOREIGN KEY (status) REFERENCES crm_statuses(crm_status_id)
我尝试在 Postgres 中 运行 一个需要交叉表的 sql 查询。我以前从未使用过交叉表查询。我的3个table如下图:
位置TABLE:
location_id、lang_id、路径
crm_statuses TABLE:
crm_status_id、crm_status_name
store_crm TABLE:
store_crm_id、status(在 crm_statuses table 的 crm_status_id 中引用),location_id(在 location_id 中引用的位置 table)
我想通过加入 store_crm table 从位置 table 获取位置作为列,或者至少将它们写成硬编码,因为它们只有 3 个(伦敦,曼彻斯特,利兹)。作为行,我想获得 crm 状态。作为内容,我想计算每个位置有多少活跃、不活跃和待定商店。活跃的、不活跃的、待定的是我的crm_statuses。所需的结果 table 将具有以下格式。
Status London Manchester Leeds
Active 2 4 5
Inactive 6 1 3
Pending 4 4 5
我怎样才能做到这一点?
您可以将城市硬编码为以下查询中的列:
SELECT
*
FROM crosstab('
SELECT
cs.crm_status_name,
l.path,
COUNT(*)
FROM store_crm AS sc
JOIN locations AS l ON (sc.location_id=l.location_id)
JOIN crm_statuses AS cs ON (cs.crm_status_id=sc.status)
GROUP BY cs.crm_status_name, l.path
ORDER BY 1,2
',
'
SELECT
path
FROM
locations
') AS f("Status" text, "London" text, "Manchester" text, "Leeds" text);
结果:
Status | London | Manchester | Leeds
----------+--------+------------+-------
Active | 1 | 2 | 1
Inactive | 2 | 1 | 4
Pending | | 1 | 1
(3 rows)
如果您有这样的数据库架构:
test=# \d+ locations
Table "public.locations"
Column | Type | Modifiers | Storage | Stats target | Description
-------------+---------+-----------------------------------------------------------------+----------+--------------+-------------
location_id | integer | not null default nextval('locations_location_id_seq'::regclass) | plain | |
lang_id | integer | | plain | |
path | text | | extended | |
Indexes:
"locations_pkey" PRIMARY KEY, btree (location_id)
Referenced by:
TABLE "store_crm" CONSTRAINT "store_crm_location_id_fkey" FOREIGN KEY (location_id) REFERENCES locations(location_id)
test=# \d+ crm_statuses
Table "public.crm_statuses"
Column | Type | Modifiers | Storage | Stats target | Description
-----------------+---------+----------------------------------------------------------------------+----------+--------------+-------------
crm_status_id | integer | not null default nextval('crm_statuses_crm_status_id_seq'::regclass) | plain | |
crm_status_name | text | | extended | |
Indexes:
"crm_statuses_pkey" PRIMARY KEY, btree (crm_status_id)
Referenced by:
TABLE "store_crm" CONSTRAINT "store_crm_status_fkey" FOREIGN KEY (status) REFERENCES crm_statuses(crm_status_id)
test=# \d+ store_crm
Table "public.store_crm"
Column | Type | Modifiers | Storage | Stats target | Description
--------------+---------+------------------------------------------------------------------+---------+--------------+-------------
store_crm_id | integer | not null default nextval('store_crm_store_crm_id_seq'::regclass) | plain | |
status | integer | | plain | |
location_id | integer | | plain | |
Indexes:
"store_crm_pkey" PRIMARY KEY, btree (store_crm_id)
Foreign-key constraints:
"store_crm_location_id_fkey" FOREIGN KEY (location_id) REFERENCES locations(location_id)
"store_crm_status_fkey" FOREIGN KEY (status) REFERENCES crm_statuses(crm_status_id)