在单个查询中对MySQL中生成的列表求和

时间:2011-09-19 09:22:33

标签: mysql sql sum

我需要总结一下我从现有查询中得到的结果。它必须扩展当前查询并保持单个查询

(我的意思是NOT - DO 1; DO 2; DO3;)

我目前的查询是:

SELECT SUM((count)/(SELECT COUNT(*) FROM mobile_site_statistics WHERE campaign_id='1201' AND start_time BETWEEN CURDATE()-1 AND CURDATE())*100) AS percentage FROM mobile_site_statistics WHERE device NOT LIKE '%Pingdom%' AND campaign_id='1201' AND start_time BETWEEN (CURDATE()-1) AND CURDATE() GROUP BY device ORDER BY 1 DESC LIMIT 10;

返回:

+------------+
| percentage |
+------------+
|    47.3813 |
|    19.7940 |
|     5.6672 |
|     5.0801 |
|     3.9603 |
|     3.8500 |
|     3.1294 |
|     2.9924 |
|     2.9398 |
|     2.7136 |
+------------+

我需要的是该表的总数(前10个设备使用的总百分比)(全部是)但它必须是单个查询(必须包含初始查询)(必须是单个查询到期)到另一个使用查询的程序)

这可能吗?到目前为止我尝试过的每一种方式都失败了。我们尝试了临时表,但这变成了多个查询。

1 个答案:

答案 0 :(得分:1)

做一个

SELECT SUM(percentage) AS total FROM (<YOUR_QUERY>) a

并将子查询<YOUR_QUERY>替换为您的初始查询