我想在COUNT中嵌入一个SELECT,但我找不到任何例子。
#pseudosql
SELECT a AS current_a, COUNT(*) AS b,
COUNT( SELECT FROM t WHERE a = current_a AND c = 'const' ) as d,
from t group by a order by b desc
答案 0 :(得分:35)
您实际上不需要子选择:
SELECT a, COUNT(*) AS b,
SUM( CASE WHEN c = 'const' THEN 1 ELSE 0 END ) as d,
from t group by a order by b desc
答案 1 :(得分:21)
您可以在子选择中移动count():
SELECT a AS current_a, COUNT(*) AS b,
( SELECT COUNT(*) FROM t WHERE a = current_a AND c = 'const' ) as d,
from t group by a order by b desc
答案 2 :(得分:1)
使用SELECT COUNT(*) FROM t WHERE a = current_a AND c = 'const' ) as d
。
答案 3 :(得分:-1)
SELECT a AS current_a, COUNT(*) AS b,
(SELECT COUNT(*) FROM t WHERE a = current_a AND c = 'const' ) as d
from t group by a order by b desc