SQL:COUNT()分组结果 [英] SQL: COUNT() grouped results

查看:317
本文介绍了SQL:COUNT()分组结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  SELECT locationname,eventid 
FROM events
GROUP BY locationname
ORDER BY locationname ASC

正如您所看到的,它按照位置名分组,因为有几行



在输出中,我只需要一个不同(分组)位置的列表,但在每个位置后面应该有每个位置的总数。所以如果在events中有4个locationsname和Congress Center NYC,输出应该是Congress Center NYC(4)。

$ p

有没有办法用COUNT()扩展查询?



谢谢!

  SELECT locationname,COUNT(h2_lin>解决方案

这是一个简单的聚合查询。 *)号
FROM事件
GROUP BY位置名
ORDER BY位置名



<如果你想要特定的格式,你可以得到它使用查询的第一行。

  SELECT CONCAT(locationname,'(',COUNT(*),')' )


this is my current query:

SELECT locationname, eventid
FROM events
GROUP BY locationname
ORDER BY locationname ASC

As you can see it's grouped by locationname because there are several rows with the same locationname.

In the output i just need a list of "distinct" (grouped) locations but behind every location there should be the total amount of each location.

So if in "events" are 4 locationsname with "Congress Center NYC" the output should be "Congress Center NYC (4)".

Is there a way to expand the query with a COUNT()?

Thanks!

解决方案

This is a straightforward aggregate query.

SELECT locationname, COUNT(*) number
  FROM events
 GROUP BY locationname
 ORDER BY locationname

If you want specific formatting, you can get it using this first line of your query.

 SELECT CONCAT(locationname, ' (', COUNT(*), ')')

这篇关于SQL:COUNT()分组结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆