[英]How to create SQL with SUM and COUNT in the same query from the same table
I have 1 table and I can run 2 separate sql queries that work well.我有 1 个表,我可以运行 2 个运行良好的独立 sql 查询。 I am trying to combine the 2 queries in to one.
我正在尝试将 2 个查询合二为一。 The queries are both using data from the same table.
查询都使用同一个表中的数据。 How to I simply combine a query the counts the number of categories and the other queries that SUMs the totals of the querys.
我如何简单地将一个查询组合起来,计算类别的数量和其他查询的总和。
See image below - on what I have when I get the 2 queries http://imgur.com/hxVRKcC见下图 - 当我收到 2 个查询http://imgur.com/hxVRKcC
I have tried to use this but it does not give me what I need我试过使用它,但它没有给我我需要的东西
SELECT category,SUM(estbudget) as estbudget,COUNT(*) as category,'count' FROM schedule GROUP BY category,estbudget ORDER by SUM(estbudget)
Here are the 2 working queries这是2个工作查询
This gives me the count of categories SELECT category, COUNT(*) from schedule group by category ORDER by COUNT(*) DESC";
这给了我类别
SELECT category, COUNT(*) from schedule group by category ORDER by COUNT(*) DESC";
This gives me the total cost per category SELECT category,SUM(estbudget) FROM schedule GROUP BY category ORDER by SUM(estbudget) DESC";
这给了我每个类别
SELECT category,SUM(estbudget) FROM schedule GROUP BY category ORDER by SUM(estbudget) DESC";
This is the outcome I would like to use with 1 query https://imgur.com/3cIhE5e这是我想与 1 个查询https://imgur.com/3cIhE5e一起使用的结果
You can try below - remove estbudget
from group by
您可以在下面尝试 - 从
group by
中删除estbudget
SELECT category,SUM(estbudget) as estbudget,COUNT(*) as 'count'
FROM schedule GROUP BY category
ORDER by SUM(estbudget)
Thanks its worked SELECT category,SUM(estbudget) as estbudget,COUNT(*) as category,'count' FROM schedule GROUP BY category ORDER by SUM(estbudget) I almost had it thanks @Fahmi感谢它的工作 SELECT 类别,SUM(estbudget) 作为 estbudget,COUNT(*) 作为类别,'count' FROM schedule GROUP BY category ORDER by SUM(estbudget) 我几乎得到了感谢@Fahmi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.