Postgresql 在具有多个子类别的postgres中返回计数为(*)的0值

Postgresql 在具有多个子类别的postgres中返回计数为(*)的0值,postgresql,crosstab,Postgresql,Crosstab,我无法使COUNT*=0显示在列中。这一问题已在一定程度上得到解决: …但我无法将解决方案推广到多个不同的类别。以下是我的情况:我有11个不同的停车位置类别和4个不同的附属类别: # SELECT DISTINCT parking_location FROM respondents; parking_location ------------------ on-street free city garage UC lot rpp vis

我无法使COUNT*=0显示在列中。这一问题已在一定程度上得到解决:

…但我无法将解决方案推广到多个不同的类别。以下是我的情况:我有11个不同的停车位置类别和4个不同的附属类别:

# SELECT DISTINCT parking_location FROM respondents; parking_location ------------------ on-street free city garage UC lot rpp visitor off-street free other nowhere other paid meter disabled rpp (11 rows) # SELECT DISTINCT affiliation FROM respondents; affiliation ------------- faculty undergrad grad staff (4 rows) 我的本科生受访者中没有一人使用残疾人停车场,因此当我试图按停车位置统计他们时,我只得到10排:

SELECT parking_location,COUNT(*) FROM respondents WHERE affiliation='undergrad' GROUP BY parking_location; parking_location | count ------------------+------- on-street free | 2 meter | 25 city garage | 5 rpp | 21 nowhere | 1012 UC lot | 33 rpp visitor | 10 off-street free | 10 other | 10 other paid | 12 (10 rows) 没问题。上述链接显示了如何显示0:

ths=# WITH c as (SELECT DISTINCT parking_location FROM respondents), ths-# r AS (SELECT affiliation, parking_location, COUNT(*) AS count FROM respondents WHERE affiliation='undergrad' GROUP BY 1,2) ths-# SELECT c.parking_location, COALESCE(r.count, 0) AS count FROM c ths-# LEFT JOIN r ON c.parking_location = r.parking_location ths-# ORDER BY parking_location; parking_location | count ------------------+------- nowhere | 1012 meter | 25 rpp | 21 rpp visitor | 10 on-street free | 2 UC lot | 33 off-street free | 10 city garage | 5 other paid | 12 disabled | 0 other | 10 (11 rows) 但现在我想让大家看看所有的附属机构,而不仅仅是本科生。此外,我想先按隶属关系排序,然后按停车位置排序生成的表。我原以为我可以删除上面的WHERE子句,但后来我的本科残疾专栏消失了:

ths=# WITH c as (SELECT DISTINCT parking_location FROM respondents), ths-# r AS (SELECT affiliation, parking_location, COUNT(*) AS count FROM respondents GROUP BY affiliation,parking_location) ths-# SELECT r.affiliation, c.parking_location, COALESCE(r.count, 0) FROM c ths-# LEFT JOIN r ON c.parking_location = r.parking_location ths-# ORDER BY affiliation,parking_location; affiliation | parking_location | coalesce -------------+------------------+---------- staff | city garage | 34 staff | other paid | 50 staff | disabled | 18 staff | other | 61 undergrad | nowhere | 1012 undergrad | meter | 25 undergrad | rpp | 21 undergrad | rpp visitor | 10 undergrad | on-street free | 2 undergrad | UC lot | 33 undergrad | off-street free | 10 undergrad | city garage | 5 undergrad | other paid | 12 undergrad | other | 10 grad | nowhere | 1113 grad | meter | 96 grad | rpp | 31 有什么帮助吗?

试试以下方法:

WITH all_parking_locations as (SELECT DISTINCT parking_location 
                               FROM respondents),

     all_affiliations as  (SELECT DISTINCT affiliation 
                           FROM respondents),

     all_counts as (SELECT affiliation, parking_location, COUNT(*) AS count 
                    FROM respondents 
                    GROUP BY affiliation, parking_location)

SELECT aa.affiliation, apl.parking_location, COALESCE(ac.count,0) as count
FROM all_affiliations aa
CROSS JOIN all_parking_locations apl
LEFT JOIN all_counts ac ON ac.affiliation = aa.affiliation
                       AND ac.parking_location = apl.parking_location
ORDER BY aa.affiliation, apl.parking_location
尝试以下方法:

WITH all_parking_locations as (SELECT DISTINCT parking_location 
                               FROM respondents),

     all_affiliations as  (SELECT DISTINCT affiliation 
                           FROM respondents),

     all_counts as (SELECT affiliation, parking_location, COUNT(*) AS count 
                    FROM respondents 
                    GROUP BY affiliation, parking_location)

SELECT aa.affiliation, apl.parking_location, COALESCE(ac.count,0) as count
FROM all_affiliations aa
CROSS JOIN all_parking_locations apl
LEFT JOIN all_counts ac ON ac.affiliation = aa.affiliation
                       AND ac.parking_location = apl.parking_location
ORDER BY aa.affiliation, apl.parking_location

将来-格式化问题中的查询并删除无用部分,如ths=。这将允许复制粘贴查询,并减少回答问题的时间。为了将来-格式化问题中的查询并删除无用部分,如ths=。它将允许复制粘贴查询并减少回答问题的时间。