将count()的结果求和

时间:2016-06-15 12:40:33

标签: sql

我有一张这样的桌子。

<authentication mode="Forms">
  <forms loginUrl="~/Account/Login" name=".ASPXAUTH" />
</authentication>

如果我想将Bekasi的块的小计计为1,雅加达的块为2,万隆的块为1,那么正确的查询是什么,所以它会返回这样的总值?

-----------------
|City    |Block |
-----------------
|Bekasi  |A1    |
|Bekasi  |A1    |  
|Jakarta |A1    |
|Jakarta |A2    |
|Bandung |A3    |
-----------------

我尝试使用此查询

-----------------
|City    |Block |
-----------------
|Bekasi  |A1    |
|Bekasi  |A1    |  
|Jakarta |A1    |
|Jakarta |A2    |
|Bandung |A3    |
-----------------
|TOTAL   |4     |
-----------------

但它只会返回这样的结果。

SELECT COUNT(DISTINCT block) AS total FROM report GROUP BY city

请帮帮我,谢谢。

3 个答案:

答案 0 :(得分:1)

这将为您提供总计数的总和

select SUM(a.total)
from 
(SELECT COUNT(DISTINCT block) AS total FROM report GROUP BY city) a

如果你想在同一个查询中获得数据和总数,你可以这样做:

select 1 as rank, city, count(city) from report group by city
union all
select 2 as rank, 'Total', SUM(a.total)
from 
(SELECT COUNT(DISTINCT block) AS total FROM report GROUP BY city) a
order by rank asc

答案 1 :(得分:0)

a = [array('i', [0, 4]), array('i', [1, 3, 20])]
#                                           ^ out of range
result = [[(X[i:]+[0])[0] for i in o] for o in a]
# [[0.1, 0.3], [0.7, 0.2, 0]]
#                         ^

答案 2 :(得分:0)

试试这个

select City,count(distinct Block) from report group by City With RollUp