简体   繁体   English

在COUNT里面选择

[英]SELECT inside a COUNT

I would like to embed a SELECT inside a COUNT, but I can't find any examples. 我想在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

You don't really need a sub-select: 你真的不需要一个子选择:

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

You can move the count() inside your sub-select: 您可以在子选择内移动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

使用SELECT COUNT(*) FROM t WHERE a = current_a AND c = 'const' ) as d

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM