计数和组合行

Counting and combining rows(计数和组合行)
本文介绍了计数和组合行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,通过向每行的国家/地区代码"列添加国家/地区代码来记录我的 Web 应用程序中的用户位置.每行代表对特定区域的访问.

I have a table that is recording user locations from my web application by adding a country code to the 'countrycode' column of each row. Each row is representing a visit to a particular area.

所以我有一些数据,比如

So I have some data like

COL1    COL2    COL3    countrycode
asd     asd     asd        NZ
asd     asd     asd        NZ
asd     asd     asd        NZ
asd     asd     asd        US
asd     asd     asd        US

我想要做的是查询这个表并在下面显示类似这样的内容

What I want to do is to query this table and show me something like this below

Country    Count
   NZ        3
   US        2

但我需要它能够为出现的更多国家/地区代码添加一行.我无法理解如何做到这一点,我知道我需要以某种方式使用 COUNT() 函数......

But I need it to be able to add a row for any more country codes that have come up. I cant get my head around a way to do this, I know I need to use the COUNT() function somehow...

推荐答案

要获得示例输出,您可以使用 GROUP BYCOUNT()

To get your example output, you can use GROUP BY and COUNT()

SELECT Country, COUNT(*)
FROM myTable
GROUP BY Country 

COUNT(*) 将计算每一行,GROUP BY Country 将按国家/地区拆分结果.结果将是基于表中数据的动态结果,因此如果您在表中添加具有不同国家/地区的更多记录,则无需更改查询.

COUNT(*) will count ever row and GROUP BY Country will split the results out by Country. The results will be dynamic based on the data in the table so you don't need to change your query if you add more records in the table with different countries.

请参阅在线图书分组依据

这篇关于计数和组合行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)
How should make faster SQL Server filtering procedure with many parameters(如何让多参数的SQL Server过滤程序更快)
How can I generate an entity–relationship (ER) diagram of a database using Microsoft SQL Server Management Studio?(如何使用Microsoft SQL Server Management Studio生成数据库的实体关系(ER)图?)