SQL总和计数结果

时间:2017-02-09 11:10:34

标签: sql

我想对计数函数的结果求和。

计数结果如下:

1. 16
2. 32
3. 0
4. 12

我想总结一下。

我试过

select sum(count(...)) ...

它不起作用。还有以下内容:

select sum(du) from (select ... as du ..)

也不工作。

1 个答案:

答案 0 :(得分:2)

您需要在内部查询select count(...) as cnt from ... group by ...中计算计数;您需要使用别名(例如cnt)来命名计数,以便您可以从外部查询引用它:

select sum(a.cnt) 
  from (
          select count(...) as cnt 
          from ... 
          group by ...
       ) as a