PostgreSQL:当x = a和x = b时,如何从表中选择2个不同的计数

use*_*252 5 sql postgresql count where having

亲爱的朋友Stackoverflowers,

如何从场景中的同一个表中选择2个不同的计数:

x=a and x=b
Run Code Online (Sandbox Code Playgroud)

具体来说,(WHEN type = subdomain)AND(WHEN subtype = subdomain)?

将它们添加到一起以创建'totalcount'?

我的尝试(供你参考):

SELECT description, type, count(1), subtype, count(2) 
FROM mapping, (dotcom WHERE type = subdomain) AS typecount, 
     (dotcom WHERE subtype = subdomain) AS subtypecount 
GROUP BY description, type, subtype 
HAVING count(1)>1 
AND count(2)>1 
ORDER BY count(*) 
DESC LIMIT 10
Run Code Online (Sandbox Code Playgroud)

第二次尝试:

SELECT description
FROM mapping, SUM(WHEN subdomain = type OR subdomain = subtype) AS count(1)
GROUP BY description, type, subtype 
HAVING count(1)>1 
ORDER BY count(*) 
DESC LIMIT 10
Run Code Online (Sandbox Code Playgroud)

com*_*ech 0

如果我正确理解你的要求,你可以这样做

SELECT description, SUM(CASE WHEN subdomain = type OR subdomain = subtype THEN 1 ELSE 0 END)
FROM mapping
GROUP BY description
Run Code Online (Sandbox Code Playgroud)